A few points:
1) while (<SFILE>) reads in the file line by line. Since you chomp the line, even the \n at the end of the line is gone. So there is no \n left to split on in your first foreach-loop. That loop can be removed without any consequence to the result
2) You split on \t</p>. That is maybe correct but will fail if there are spaces too. If your source file is machine generated you might be able to guarantee that condition, if the file is edited by hand, splitting on <c>/\s+/ will make more sense. It will split on any combination of multiple tabs and spaces
3) If you have unwanted tabs and spaces at the end of the lines of the input file, use $line=~s/\s+$//;. If you don't want a final tab in your output because of your print "$x\t$y\t"; then you might use the following instead:
my @result; foreach my $c (@array) { next if $b =~ /^Individual/; next if $b =~ /^000\d+/; next if $b =~ /^\w+/ ; ($loga, $logb, $x, $y) = split (/ /,$c); push @result,$x,$y); } print join("\t",@result);
4) You seem to check that if a number has 3 digits then it must be the number of an individual. And you used the wrong regex for it. if ($a =~ /^\d\d\d$/){ should work, but if any other data ever has 3 digits you would have a problem. As an alternative you could check each line if it begins with the string 'Individual' and depending on that go into different loops. Your pogram would have the following structure:
while (<SFILE>) { ... if ($line=~/^Individual) { my @array= split (/\s+/, $line); shift @array; #removes the 'Individual' string foreach (@array) { ... #process an 'Individual' line } } else { my @arrays= split (/\s+/, $line); my $year= shift @array; foreach (@array) ... #process a data line } }
And a final general hint, if it happens that you don't know what your program is doing, insert meaningful print-lines, for example print "Starting loop 2, \$a=<$a>\n";
In reply to Re: rearranging the file
by jethro
in thread rearranging the file
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |