Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Looping backwards through two arrays at once?

by ikegami (Patriarch)
on Nov 04, 2005 at 17:04 UTC ( [id://505784]=note: print w/replies, xml ) Need Help??


in reply to Looping backwards through two arrays at once?

"Perl-style" for loop:

for my $i (reverse 0..$#a) { print("$a[$i], $b[$i]\n"); }

"C-style" for loop:

for (my $i=@a; $i--; ) { print("$a[$i], $b[$i]\n"); }

Update: Fixed crossed wires.

Replies are listed 'Best First'.
Re^2: Looping backwards through two arrays at once?
by Limbic~Region (Chancellor) on Nov 04, 2005 at 17:14 UTC
    ikegami,
    Your suggestion of a C style for loop as an alternative feels odd to me. I would have written it as a while loop as it feels more natural.
    my $i = @array; while ( $i-- ) { # ... }

    Cheers - L~R

      I prefer for over while because

      • the index is scoped to the loop, and
      • I find it more readable since the bounds of the loop are on a single line.
Re^2: Looping backwards through two arrays at once?
by kutsu (Priest) on Nov 04, 2005 at 17:19 UTC

    umm....don't you mean for my $i (reverse 0 .. $#a) on the first example?

    Update: Already corrected it, it seems :).

    "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

Re^2: Looping backwards through two arrays at once?
by holli (Abbot) on Nov 04, 2005 at 19:03 UTC
    From the belly I would have said that
    for my $i (reverse 0..$#a)
    would cast the list into existance, but as it seems that isn't the case (Perl 5.8).


    holli, /regexed monk/

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://505784]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-19 15:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found