Re: Line number problem with foreach
by BrowserUk (Patriarch) on Feb 28, 2009 at 04:43 UTC
|
for takes a list, so it reads the entire file before iterating the loop. So, by the time you get to printing the lines within the loop, $. is set to the line number of the last line in the file. Using while instead of for will ensure that you process the file line by line.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
|
|
BrowserUK is exactly right. The foreach() loop asks for a list. The <> diamond operator behaves differently in list context than it does in scalar context. In scalar context, each time the diamond operator is evaluated it hands you the next line of the file. In list context, the entire file is slurped into memory at once and a list is created (and enumerated for the foreach loop).
The $. special variable tells you what line of a file you most recently read. The foreach loop only evaluates the <> operator one time -- when it slurps in the entire file. Each time through the loop the foreach operator hands you the next line of the list, but the entire file has already been read into memory, and the $. variable tells you this by telling you that you're on line 4.
This is a valuable lesson on how to quietly build a piece of code that cannot scale well when file sizes grow. If it weren't for the fact that the $. variable did something you didn't anticipate, you might have left the foreach loop in your code. Then one day when the file is 2GB huge, your client would call and ask why the program is locking up his system.
Sticking to the while() construct will eliminate that issue.
| [reply] [d/l] [select] |
Re: Line number problem with foreach
by dharanivasan (Scribe) on Feb 28, 2009 at 08:05 UTC
|
This is equivalent to while(<>) { ... }
use strict;
use warnings;
open my $fh, "test.txt" or die $!;
foreach (;<$fh>;){
print " $. :$_ \n";
}
| [reply] [d/l] |
|
|
Very interesting. I tried your code, and you are right. $. contains the current line number for each record in the input file. My question is "Why does the presence of semi-colons around the diamond make this work? Do they force scalar mode instead of list mode? Does this mean the file is NOT slurped into a list all at once?
"Its not how hard you work, its how much you get done."
| [reply] |
|
|
foreach (;<$fh>;) {
print " $. :$_ \n";
}
the initialization and finalization clauses of the loop (the parts before the first semicolon and after the second semicolon, respectively) are empty. The only clause specified is the conditional clause (between the two semicolons). When the <> or readline operator is evaluated in the scalar context of the conditional, it returns a single line at a time until the file is exhausted, when it returns undef.
This is equivalent to reading <$fh> in a while-loop. | [reply] [d/l] [select] |
|
|
Well, this is indeed a for loop
| [reply] |
Re: Line number problem with foreach
by leslie (Pilgrim) on Feb 28, 2009 at 04:55 UTC
|
Hi ree,
foreach:
First it will fetch all the data from the file and store it into the buffer, then from the buffer only it will print the data. So while storing the buffer it self line number($.) getting incremented. So finally it will have last line number only. So while printing it will print last line number.
Instead of foreach you can use while it will print
correct line number.
| [reply] |
Re: Line number problem with foreach
by lakshmananindia (Chaplain) on Feb 28, 2009 at 04:58 UTC
|
use strict;
use warnings;
my @a=(1,2,3,4,5);
foreach (@a)
{
print $_;
}
The foreach will accept a list so that the foreach expands as follows.
foreach (1,2,3,4,5);
Similar to that while reading the file contents, it will read the whole file content and $. will set to the last line number.
| [reply] [d/l] [select] |
Re: Line number problem with foreach
by bruno (Friar) on Feb 28, 2009 at 04:36 UTC
|
open my $fh, "test.txt" or die $!;
while(<$fh>) {
print "$.:$_";
}
__END__
outputs:
1:First line
2:Second line
3:Third line
4:Fourth line
| [reply] [d/l] |
|
|
| [reply] |
Re: Line number problem with foreach
by jack47 (Initiate) on Feb 28, 2009 at 15:45 UTC
|
You need to use a while rather than a foreach here.
The foreach slurps the entire file into an array, then sets $_ to each element of the array in turn... not what you want. The while(<$fh>) reads one line at a time and sets $_ to that line. | [reply] |
Re: Line number problem with foreach
by Anonymous Monk on Mar 01, 2009 at 05:29 UTC
|
Oh, pick me, pick me! I would love to be the eighth or ninth person to offer the same answer with no added value just for XP! | [reply] |
Re: Line number problem with foreach
by justlook (Initiate) on Feb 28, 2009 at 13:18 UTC
|
you mast do this
open my $fh,"<test.txt" or die $!;
while(<$fh>){
print "$.:$_";
}
| [reply] |