[HRPM] nother one

Mark D Wolinski mark at mrmark.com
Wed Nov 15 15:25:41 CST 2000


The only way you could do it is something like this:

$i=0;
foreach (@array) {
 if ($_ = $array[$i+1]) {
   one forward compare
 }
 $i++;
}

But you have to make sure you don't compare @array+1.  And if you do it this
way, then you have to ask yourself why you don't really want to do a for
loop.  Chris could probably give a technical answer if a for loop is more
expensive then a foreach or not...

Mark W


-----Original Message-----
From: owner-norfolk-pm-list at pm.org
[mailto:owner-norfolk-pm-list at pm.org]On Behalf Of Troy E. Webster
Sent: Wednesday, November 15, 2000 1:20 PM
To: norfolk-pm-list at happyfunball.pm.org
Subject: [HRPM] nother one


Hey,

This may turn out to be a stupid question, but nothing ventured nothing
gained...

Is there a way to peek ahead in a foreach loop?

For example, foreach $elem (@array){}
can I look at $elem+1 while looking at $elem? I realize I can do the same
type of loop using a while loop and then checking $array[i+1] (where i is
the current element) but I was curious if there was a way to do it with
a foreach loop.

Thanks
Troy

___________________________________________________________________________
``Beware of bugs in the above code; I have only proved it correct, not
tried it.''  -Don Knuth
---------------------------------------------------------------------------




More information about the Norfolk-pm mailing list