in reply to Re: Mini-Tutorial: Working with Odd/Even Elements
in thread Mini-Tutorial: Working with Odd/Even Elements
Update: figured it out (simulthanks to Ikegami). Gotta reference the right package. Corrected solution:
Caution: Contents may have been coded under pressure.
Previous, erroneous solution follows.sub map_pairs(&@) { my $fn = shift; my $pkg = caller; map { my $idx = $_ * 2; no strict 'refs'; my ($a, $b) = local (${$pkg.'::a'}, ${$pkg.'::b'}) = (@_[$idx,$idx+1]); $fn->($a, $b); } (0..$#_/2); }
I think you made this harder than it needs to be. Isn't this equivalent?
sub map_pairs(&@) { my $fn = shift; map { my $idx = $_ * 2; local ($a, $b) = @_[$idx,$idx+1]; $fn->($a, $b); } (0..$#_/2); }
Caution: Contents may have been coded under pressure.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Mini-Tutorial: Working with Odd/Even Elements
by ikegami (Patriarch) on Jul 10, 2009 at 18:56 UTC |
In Section
Meditations