Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Iterating over verbatim hash reference

by LanX (Saint)
on Jan 23, 2010 at 01:45 UTC ( [id://819102]=note: print w/replies, xml ) Need Help??


in reply to Iterating over verbatim hash reference

Easily extending foreach to multiple iterator-vars is a problem in perl5.

That's why the for syntax was extended in perl6

for %hash.kv -> my $key, $val { say "$key corresponds to $val"; }

Me myself I would love to have a real macro mechanism to solve this properly in perl5, anyway I can suggest you some code to simulate it at least in a functional way

use strict; use warnings; { my %lists; sub deal { my $aref=shift; my $id= \$_[0]; #print $aref,$id; $lists{$id}=$aref if (! exists $lists{$id} ); if (@{$lists{$id}}){ for(@_) { $_=shift @{$lists{$id}}; } return 1; # could be extended to # * iteration counter # * remaining list-elements # * a list of all of these } else { delete $lists{$id}; return 0; } } } $\="\n"; #--------- here we go while (deal [1..9] => my ($a,$b,$c) ){ print $a,$b,$c; while (deal [a=>1,b=>2] => my ($a,$b) ){ print $a,$b; } } #--------- print "EXIT"; __DATA__ 123 a1 b2 456 a1 b2 789 a1 b2 EXIT

I'm not sure if it works prior to 5.10, IMHO there was an issue about "late" my declarations.

Some things to note:

  1. The anonymous array will be build each time, but ignored after the first iteration.
  2. my first guess was to call the function "iter", but "deal" (like to deal cards to players) should make clear that it's not a lazy evaluated iterator-function!
  3. the reference of the first iterator-variable (here \$a) is taken as identifier to avoid confusion when nesting iterations, like with $_ when foreaching you should never mess around with the same instance of $a.
  4. you are flexible to use as many iterator-vars as you want, not only two. Taking one simulates foreach, with the advantage that gotos into the body are possible.
  5. the fat comma => is just syntactic sugar, but I think its more readable like this (and even a little more intuitive than ikegamis suggestions).
  6. the performance problem with rebuilding the list at each pass may be solved by passing a code-block,  sub {LIST} which is only once evaluated internally. When using prototype (&@) even the sub could be omitted².

Other approaches¹ solving the repeated LIST creation may be to split functionality onto two functions deal and to such that either

 for (deal(LIST);to(VARS)) { ... }

or

 while ( deal(LIST) .. to(VARS) ) { ... }

work! (But error-handling becomes trickier)

In the latter correct return values would make the flip-flop operator (scalar ..) build the list only once!

Cheers Rolf

UPDATES:

¹) thats really complicated to realize avoiding a nesting mess, because it's not possible to identify from which codeposition to() is called, the linenumber in caller is not sufficent.

²) maybe the best solution! The call syntax for iterating over a literal hash would be

while( deal {a=>1,b=>2} my($k,$v) ) {..}
Please note, no (fat) comma needed, because now we are passing code returning a list NOT a hashref! That's analogous to map and grep's syntax.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-03-28 16:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found