Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^2: Perlplexation - foreach shoulda Known

by JavaFan (Canon)
on Apr 13, 2012 at 20:29 UTC ( #964987=note: print w/replies, xml ) Need Help??

in reply to Re: Perlplexation - foreach shoulda Known
in thread Perlplexation - foreach shoulda Known

Is there a special variable that tracks what $i is tracking in that example?
No. If you want that, use a C-style for loop. A reason not to have an special variable with an interation counter: how should that work with nested loops?
  • Comment on Re^2: Perlplexation - foreach shoulda Known

Replies are listed 'Best First'.
Re^3: Perlplexation - foreach shoulda Known
by Riales (Hermit) on Apr 13, 2012 at 20:41 UTC

    I would think it could work the same way the $_ variable works in nested loops...

      Ah, you mean, "$_ is the default iterator value, but it's easily replaced by another variable"? How should that work for the loop counter?

        Right...I think. If I need to use the value of $_ in a nested loop, I'd just assign it to another variable:

        for (0 .. 10) { my $outer_elem = $_; for (0 .. 20) { my $inner_elem = $_; do_something($outer_elem, $inner_elem); } } # Yeah, I know doing this is better: for my $outer_elem (0 .. 10) { for my $inner_elem (0 .. 20) { do_something($outer_elem, $inner_elem); } }

        So I guess the loop counter would just keep a count of the most immediate loop it's in. I don't really know--this is all just how I would expect such a variable to work (if it existed).

        So if we pretend $. is the loop counter:

        foreach my $elem1 (@array1) { my $outer_idx = $.; foreach my $elem2 (@array2) { my $inner_idx = $.; # Blablabla... } }

        I should emphasize: I'm not pushing for this to become a feature or anything. I was just curious if there was anything like this (and this is how I would imagine it might work if there was).

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2018-11-15 10:18 GMT
Find Nodes?
    Voting Booth?
    My code is most likely broken because:

    Results (182 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!