Re: which loop is better
by muntfish (Chaplain) on Oct 04, 2004 at 08:32 UTC
|
perldoc perlsyn says that they are
synonyms. So the behaviour should be identical. If
you're seeing something different, perhaps you could
post (a small amount of) code to demonstrate this?
s^^unp(;75N=&9I<V@`ack(u,^;s|\(.+\`|"$`$'\"$&\"\)"|ee;/m.+h/&&print$&
| [reply] [Watch: Dir/Any] [d/l] |
Re: which loop is better
by muba (Priest) on Oct 04, 2004 at 08:30 UTC
|
I once wondered this too. for and foreach seem to be exactly the same, so I think it's weird they behave different in your case.
| [reply] [Watch: Dir/Any] [d/l] |
Re: which loop is better
by Jasper (Chaplain) on Oct 04, 2004 at 13:02 UTC
|
Please post your code that caused the wrong elements to be taken. I bet there were other differences, apart from 'each'. | [reply] [Watch: Dir/Any] |
Re: which loop is better
by chromatic (Archbishop) on Oct 04, 2004 at 23:10 UTC
|
| [reply] [Watch: Dir/Any] [d/l] |
Re: which loop is better
by Anonymous Monk on Oct 04, 2004 at 12:28 UTC
|
Why would you even ask such a question? It implies that while Larry was constructing Perl, he was thinking "hmmm, I've a couple of looping constructs. One is clearly better, but let's put the inferior constructs in Perl as well".
You can't say "construct A is better than construct B" - if that were true, we wouldn't have construct B in the first place. Sure, it might be that A is better than B for doing task T, but that's for a specific task. For a different task, B might be better (after specifying by which measure you want it to be "better") | [reply] [Watch: Dir/Any] |
|
I dissagree.
There are cases where foreach (LIST) is better than for (EXPR;EXPR;EXPR), but the 3 argument for can do things that foreach can't. When I program in C or Java 50-80% of my for loops are itterating over an array (though use itterators far more in Java), what I would use foreach to do in Perl. In the case of itterating over every element of an array foreach more accurately expresses my intent. I could use for, and it would work, but for tends to be more cluttered, and therefore IMO less readable.
foreach (@array) {
dosomethingto($_);
}
is basicly the same as
for (my $i = 0; $i < @array; $i++) {
dosomethingto($array[$i]);
}
I find the first simpler and the second more powerful but more noisy for most uses.
If you really feel that Larry never includes 'inferior constructs' in Perl then you don't see any thing wrong with writing
my $fs = '';
for (my $f = 0; $f < 10; $f++) {
#last if $f == 7;
next if $f == 5;
print "f: $f\n";
$fs .= "$f ";
redo if length($fs) == 6;
}
print "fs: $fs\n";
as
my $gs = '';
FOR_INIT:
my $g = 0;
goto FOR_TEST;
FOR_NEXT:
$g++;
FOR_TEST:
goto FOR_LAST unless $g < 10;
FOR_REDO:
#goto FOR_LAST if $g == 7;
goto FOR_NEXT if $g == 5;
print "g: $g\n";
$gs .= "$g ";
goto FOR_REDO if length($gs) == 6;
goto FOR_NEXT;
FOR_LAST:
print "gs: $gs\n";
They do the same thing, and I understand both of them. So obviously since they are both in Perl they are both equaly correct. </sarcasm>
My guess is Larry included both 'better' and 'inferior' constructs because sometimes better is too specific for a task, and only inferior will do the job (such as Switch's use of goto). And it should be up to the programmer to know which to use. | [reply] [Watch: Dir/Any] [d/l] [select] |
Re: which loop is better
by TedPride (Priest) on Oct 04, 2004 at 10:17 UTC
|
There's no real difference in speed. The difference is in structure - for allows you to initialize, increment, and test, while foreach just increments through a list of items. The following are equivalent:
foreach (@arr[0..2]) { $_++; }
for (my $i = 0; $i <= 2; $i++) { @arr[$i]++; }
The latter offers more control, but isn't necessary for most looping purposes. | [reply] [Watch: Dir/Any] [d/l] |
|
for and foreach are synonyms. there really is no difference between them:
for (@arr[0..2]) { $_++; }
foreach (my $i = 0; $i <= 2; $i++) { @arr[$i]++; }
will work just as well
| [reply] [Watch: Dir/Any] [d/l] |
|
Err, I don't think what you say is very clear. For and foreach are synonyms, but
either of them can be used to delcare two completely
different loop contructs, ie the following two lines are
identical to each other
for (expr; expr; expr) block
foreach (expr; expr; expr) block
while the following two are identical to each other, but not
to the first two lines above
for (list) block
foreach (list) block
Dave. | [reply] [Watch: Dir/Any] [d/l] [select] |
|