#!/usr/bin/perl use strict; # read the file name and create new files name my $a=$_=shift; s/\.csv/\.dat/; my $b=$_; #s/\.dat/\.ind/; #my $c=$_; system("touch $b");# invoke the system function my $index=0; my $init; my $end; my $newline; my $flag=1; my $m; my @field; # open the file and output the field name open (CSV,"<",$a) or die "something wrong\n"; print "the field name of this file follow:\n"; my $line=; print $line; chomp($line); # if need to restruct the file, following code is useful #print "Do you need to restruct the file?Y[y] or N[n]\n"; #my $char=<>; #if ($char=~/y/i) # { @field=split/,/,$line; print "input the start field name\n"; my $st_name=<>;chomp($st_name);#print "new name $st_name\n"; print "input the end field name\n"; my $en_name=<>;chomp($en_name); my $index=@field;$index=0; while ($flag) { #print "$field[$index]\n"; if ($st_name eq $field[$index]) {$init=$index;} if ($en_name eq $field[$index]) {$end=$index;$flag=0;} if ($index==$#field) {$flag=0;$end=$index;} $index++; } #$newline=$end-$init ; # } #print "new line $newline\n"; # create .dat and .ind #open (NEWIND,">>",$c) or die "can't create the file .ind\n"; open (NEWDAT,">>",$b) or die "can't open the file\n"; #print"add comments and new field name to .dat file,type \"exit\" to exit\n"; #while (<>) #{ if ($_=~/exit/){ last;}print NEWDAT ;} print "write the field name\n"; my $xhx=<>; #chomp($xhx); print NEWDAT $xhx; print NEWDAT "#".$line."\n"; #insert new lines while ($line=) { #print "old line is $line\n"; #if ($char=~/y/i) chomp($line); my @insert=split/,/,$line;#print "@insert\n"; #print "the $init, $end\n"; for(my $index1=$init;$index1<=$end;$index1++) { $m=$insert[0];#print "$m\n"; for ($index=1;$index<$init;$index++) {$m=join ",",$m,$insert[$index];} $m=join ",",$m,$field[$index1];$m=join ",",$m,$insert[$index1]; for (my $index=$end+1;$index<@field;$index++) {$m=join ",",$m,$insert[$index];} print NEWDAT $m."\n"; } # } #else {print NEWDAT $line;} # print "new line is $m\n"; }