in reply to Iterating over verbatim hash reference
A little explanation: I'm defining a recursive anonymous sub, that gets its coderef as its first argument, and the hash listified as the following arguments. Then returns undef for the recursion base case (@_==1), or returns an arrayref for its last two args, and calls itself for the remaining args (if there are enough remaining args). Okay, try it, the result is this:#! /usr/bin/perl use strict; use warnings; my $f = sub { return @_ == 1 ? undef : ( [ splice @_, -2 ], @_ == 1 ? () : &{ $_[0] }(@_) ); }; map { print "$_->[0] -> $_->[1]\n" } $f->( $f, %{ { x => 5, y => 8, z => 42 } } );
Then eliminate the var for the coderef: replace the occurances of $f in the map statement with the former sub{} definition and we get this:z -> 42 x -> 5 y -> 8
I'm sure you don't want to use this in real code, however this shows the idea. And sorry but I cannot see any easier solution at this moment.#! /usr/bin/perl use strict; use warnings; map { print "$_->[0] -> $_->[1]\n" } &{ sub { return @_ == 1 ? undef : ( [ splice @_, -2 ], @_ == 1 ? () : &{ $_[0] }(@_) ); } }( sub { return @_ == 1 ? undef : ( [ splice @_, -2 ], @_ == 1 ? () : &{ $_[0] }(@_) ); }, %{ { x => 5, y => 8, z => 42 } } );
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Iterating over verbatim hash reference
by rovf (Priest) on Jan 22, 2010 at 11:04 UTC | |
by rubasov (Friar) on Jan 22, 2010 at 12:31 UTC | |
by rovf (Priest) on Jan 22, 2010 at 14:02 UTC |