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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.