Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Recursively walk a hash to get to an element

by tlm (Prior)
on Mar 31, 2005 at 02:00 UTC ( #443660=note: print w/replies, xml ) Need Help??

in reply to Recursively walk a hash to get to an element

There's an typo in the code; the code block in the reduce statement should be

{ \($$a->{$b}) }
Also, when I tried the code as given (after fixing the above typo), I got the error:
% perl Can't call method "List::Util::reduce" on unblessed reference at rec_w line 9.
Here's the full code of the script that caused that error:
use strict; my %myhash; $myhash{bedrock}{flintstone}{fred} = 3; my $ref = pointer_to_element(\%myhash, qw(bedrock flintstone fred)); sub pointer_to_element { require List::Util; return List::Util::reduce { \($$a->{$b}) } \shift, @_; }
I couldn't figure out why the error, so I switched the code to
use List::Util 'reduce'; sub pointer_to_element { return reduce { \($$a->{$b}) } \shift, @_; }
and it worked great. Tres cool.

the lowliest monk

Replies are listed 'Best First'.
Re^2: Recursively walk a hash to get to an element
by ihb (Deacon) on Mar 31, 2005 at 03:27 UTC

    The reason of the error is that reduce uses a prototype not yet seen when the return statement is compiled. It's therefore interpreted as indirect object syntax (method { STATEMENTS } LIST), being do { STATEMENTS }->method(LIST) with direct syntax. The way to fix this is to call &reduce like List::Util::reduce(sub { ... }, LIST).


    See perltoc if you don't know which perldoc to read!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://443660]
[Corion]: Hi Discipulus!
[Discipulus]: your chautauqua was important for me
[Discipulus]: Hi Corion here is holiday: the best we have Liberation Day
[Discipulus]: waiting sons to finish home assignments and we go out to some park
[Corion]: Discipulus: Oh ;) I'm looking forward to 1st of May - the next holiday here ;)

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2017-04-25 11:12 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (449 votes). Check out past polls.