Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^5: Perlplexation - foreach shoulda Known

by Riales (Hermit)
on Apr 13, 2012 at 22:04 UTC ( #965005=note: print w/ replies, xml ) Need Help??


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

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).


Comment on Re^5: Perlplexation - foreach shoulda Known
Select or Download Code
Replies are listed 'Best First'.
Re^6: Perlplexation - foreach shoulda Known
by JavaFan (Canon) on Apr 14, 2012 at 01:19 UTC
    If you're willing to scribble away the value of $. in a variable at the beginning of the body, you might as well write:
    my ($i, $j); foreach .... { ++$i; foreach .... { ++$j; } }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (11)
As of 2015-07-28 11:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (254 votes), past polls