drieux,

This came just to me - I presume you meant to go to the list too?

thanks paul... must have missed one step in the responding...

On Thu, Dec 18, 2003 at 06:36:40PM -0800, drieux wrote:

On Dec 18, 2003, at 4:43 PM, Paul Johnson wrote:
On Thu, Dec 18, 2003 at 04:31:20PM -0800, Jeff Westman wrote:
Eric Walker <[EMAIL PROTECTED]> wrote:

I got it so I need a counter which sends me to a for loop instead of
a
foreach.
[..]
I believe that 'for' and 'foreach' are completely interchangable. I
remember
reading somewhere that one was a synonym for the other.

That is correct, though often people will refer to foreach meaning


foreach VAR (LIST) BLOCK

and to for meaning

for (EXPR; EXPR; EXPR) BLOCK

perldoc perlsyn
[..]

Minor point, while it is true, per the pod, that
the foreach is a synonym, there is the little difference
that one does NOT get an 'index' of which element of
LIST is the current VAR when using the foreach.
So the transparency is more towards say

        my @list = qw/bob ted carol alice/;
        for ( my ($i,$var) = (0,$list[0]); $i < scalar(@list); $var =
$list[++$i] )
        {
                print "var is $var\n";
        }

hence why the general habit of

        foreach my $var (@list)
        {
                print "var is $var\n";
        }

when all one wants to do is simply walk through the LIST
in straight linear order.

The moment that one Needs to know the 'index' into an
array to look ahead or behind, one of course needs to
do something like

        my ($index, $max ) = ( -1, scalar(@list));
        while(++$index < $max )
        {
                print "var at $index is $list[$index]\n";
        }

so what one needs when iterating through an array
is an index into that array, which can be implemented
in a for loop or a while loop or a do_while or...

ciao
drieux

---

-- Paul Johnson - [EMAIL PROTECTED] http://www.pjcj.net



ciao
drieux

---


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to