Description: | In this posting to the Fun With Perl mailing list, I presented a technique for iterating over the elements of a hash, in much the same way that map iterates over the contents of an array. Yes, you can use map directly, but that means collecting all the keys of the hash up front, and that could be undesirable if the hash is large, or tied. |
sub hasheesh(&\%) { my( $c, $h ) = @_; local( $a, $b ); @_ = (); while ( ( $a, $b ) = each %$h ) { push @_, $c->(); } @_ } # you'd use it like this: hasheesh { print "$a => $b\n" } %h; # or: print hasheesh { "$a => $b\n" } %h;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: map-like hash iterator
by jdporter (Chancellor) on Nov 06, 2002 at 20:15 UTC | |
by ikegami (Patriarch) on Mar 26, 2010 at 23:11 UTC | |
Re: map-like hash iterator
by particle (Vicar) on Nov 06, 2002 at 23:32 UTC | |
by jdporter (Chancellor) on Nov 07, 2002 at 00:00 UTC | |
Re: map-like hash iterator
by Aristotle (Chancellor) on Nov 06, 2002 at 20:19 UTC | |
by jdporter (Chancellor) on Nov 06, 2002 at 20:51 UTC | |
by Aristotle (Chancellor) on Nov 06, 2002 at 21:26 UTC | |
by jdporter (Chancellor) on Nov 06, 2002 at 21:32 UTC | |
by Aristotle (Chancellor) on Nov 06, 2002 at 23:19 UTC | |
|