Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

foreach versus while 3

Status
Not open for further replies.

nfaber

Technical User
Oct 22, 2001
446
US
Hello All,

Quick question. Can someone tell me the difference between a foreach and while loop? They seems to work the same way.

Nick
 
Foreach takes an array, and returns all the values of the array in a scalar:

@interests=("golf","tennis","fencing","blacksmiths");
foreach $interest (@interests)
{
print $interest . ".<br>";
}

yields:

golf.
tennis.
fencing.
blacksmiths.
 
So how does the while loop do it differently?
 
A while loop can be used for the same purposes as a foreach loop but it can also be used to repeat a function over and over again until a certain condition exists:

$number=50;
while($number!=97)
{
$number++
}
 
Aarem's got it pretty much when he says
Foreach takes an array, and returns all the values of the array in a scalar
A while generally does not do this. It simply tests whether some condition is true. For example:
Code:
@interests=("golf","tennis","fencing","blacksmiths");
for (@interests) {
    # each elem of @interests returned 1 at a time in $_
    ...
}

while (@interests) {
    # Does not return a list element.  
    # Just tests for "true" (non-zero/non-null) value.
    # Results in [b]endless loop[/b] if [b]@interests[/b] 
    # is not empty and not modified within the loop
    ...
}
The exception, where while behaves the same way as for, is the while (<FH>) form, where the lines of FH are returned one at a time in $_. (while (<>) does this for all files named on command line, with no need to explicitly open them.)

In many cases, a loop written with for could be rewritten with while or vice versa, but one or the other is usually more convenient. Consider, for example, rewriting the above for loop with a while:
Code:
my $i = 0;
while (defined($interests[$i++])) {
    ...
}
HTH



 
Hmm, make that
Code:
my $i = 0;
while (defined($interests[[b]$i[/b]])) {
    # Do something with [b]$interests[$i][/b]
    ...
    [b]$i++;[/b]
}
Like I said, one or the other is usually more convenient. And less error-prone. :)

 
while is also easier to use to loop through a hash than a foreach

Code:
#foreach
foreach $keys (keys(%hash)) {
     # gives you only keys.  With values, the foreach loop
     # gets even more complicated.
}

Code:
# while
while (($keys,$values) = each(%hash)) {
     # lets you use both values of a hash, more easily
}
 
thanks for the replies. I get it now.
 
Just one comment on mikevh's point about while(<FH>)

Although that seems to behave similarly to foreach(<FH>), they're actually not the same. "while" will read one line from the file on each iteration, thus saving memory. "foreach", on the other hand, will slurp the entire file into memory and then iterate over the lines of the in-memory version of the file.
 
Thanks, ishnid, never thought of that.

I almost always use while to loop over a file, though this is mostly out of habit rather than because of any awareness of memory considerations.

The exception would be something like
for (sort <FH>)
where you'd need the whole file in memory to do the sort.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top