in reply to Iterating single line within a For Loop

As BrowserUk says, for(<INFILE>) ... slurps the whole file before starting to iterate.. This means that you've already run out of potential input by the time you get to my $seq = <INFILE>;. If you say
while (<INFILE>) { if (/^@(\S+)/) { print "$1\n"; my $seq = <INFILE>; } }
then my $seq = <INFILE>; will grab the line after a line starting with @ because the while test is run once per iteration. A for loop constructs the entire list first, which means it pulls in all lines before the first iteration.

Also note that, while you are fine here, an unescaped @ in a regular expression will usually end up looking like an array in Perl; it's probably a good idea to get in the habit of escaping them.


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^2: Iterating single line within a For Loop
by diyaz (Beadle) on Feb 25, 2015 at 19:20 UTC
    Thank you both! That is very helpful to know!