A vs foreach when reading from STDIN

Possible duplicate:
What is the difference between iterating over a file using foreach or in Perl?

In a while loop, Perl reads an input line, puts it in a variable, and starts the body of the loop. Then it returns to find another line of input. But in a foreach loop, linear input is used in the context of a list (since foreach needs a list to iterate over). Therefore, it must read the entire input before the cycle can start working. This difference will become apparent when the input comes from your 400 MB web server log file! It is generally best to use code, for example, a fast cycle loop that will handle entering a string at a time when possible. Source - Llama's Book

So what should I use while every time? What are the possible situations where the foreach will dominate what?

+4
source share
3 answers

As you point out, iterating over a file using foreach interrupts the file in memory before the loop starts.

Although it is true that one of Perl's crosses is β€œThere is more than one way to do this,” there are ways to do things that have no real benefit compared to any other way. foreach is probably a bad alternative for looping around a file. If you are going to iterate over a file line by line, you usually use while() . If you are going to decompose the entire file into memory at once, you do it in one step and execute it.

Sometimes it is useful to rip the whole file. For example, one of them minimizes file open time. Most often, this is convenient for working with data that does not really correspond to a string record or recording by a recording model. foreach () deletes the file, but actually does not give a good opportunity to close it immediately, so the good will disappear. And looping through a file using foreach will not help you if your data should be considered as a piece. So the reason is gone too. In the end, you get a negative file breakaway, without any positive effects for file corruption.

There will always be someone who comes up with a smart reason that others have not considered. But I have not seen it yet. (Usually, when I say something like this, someone writes β€œthis,” so it’s always dangerous to assume that he has absolutely no merit.) Let it be so: until you find one of the very few For good reasons using foreach to iterate over a file, don't worry about it. Of course, when you make this discovery, you will realize that the time has come.

+6
source

The general rule is to use while if the item you are working with changes during its use:

 while(my $line = <>) while(my ($k, $v) = each %h) 

but for or foreach otherwise:

 foreach my $line (@lines) for my $k (keys %h) for(my $i = 0; $i < $pancakes; ++$i) 

In the case of a file, using while makes sense, because you (probably) do not want to trick it all into memory (except, of course, when you want). But if you already have data in memory, then for / foreach would be a more natural choice to iterate over it.

+5
source

You use foreach to iterate over each element of the array or each hash key when the data is already in memory. You use while when the data is not yet in memory (and you might not even need to store everything in memory at once).

 my %hash; while (<>) { chomp; my($key, $value) = split /,/; $hash{$key} = $value; } foreach my $key (sort keys %hash) { print "$key => $hash{$key}\n"; } 
+2
source

All Articles