MUBA,
Ok - yes and no - depends on how you look at it. One is usually referred to as a C-style for loop (does not localize $_ or implicitly alias looping variable) and the other is typically referred to as a foreach loop (which does both of those things).
for ( $i = 0; $i < 10; $i++ ) {
# C-style but could also say foreach
}
foreach ( @array ) {
# Localizes $_ and implicitly aliases looping variable
# Could also just be for
}
In your code, it does not matter which name you use. It is the looping construct that follows the name that determines the internal behavior.
Cheers - L~R
| [reply] [d/l] |
Camel Book, 3rd Edition, page 118 says, "The foreach keyword is just a synonym for the for keyword, so you can use for and foreach interchangeably, whichever you think is more readable in a given situation."
So you could do:
#!/usr/bin/perl
use strict;
my @array = qw(one two three four);
print "Foreach:\n";
foreach my $item (@array) {
print "$item\n";
}
print "\nFor:\n";
for my $item (@array) {
print "$item\n";
}
Same thing... | [reply] [d/l] |
Check out for, this has been discussed many times before -- get to know super search. | [reply] |
for and foreach are the same.
| [reply] [d/l] [select] |
| [reply] |