rvosa has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

I'm trying to apply slicing semantics to a hash reference, analogous to the following:
%hash = ( 'a' => 1, 'b' => 2 ); print @hash{'a','b'}; # prints '12'
Now I want to do the same with a hash reference:
$hasref = { 'a' => 1, 'b' => 2 }; # not sure... is it $hashref->{'a','b'}? That just yields 'uninitializ +ed'.
Thanks!

Replies are listed 'Best First'.
Re: slicing a hash ref
by Sidhekin (Priest) on Jul 01, 2006 at 02:59 UTC

    There is no arrow syntax, I'm afraid -- you'll have to deref with the sigil:

    print @$hashref{'a','a','b'};

    If your hashref is the least bit complicated, you'll want to use a block. In fact, you might want to use a block in any case, rather than worry about precedence ...

    print @{$hashref}{'a','a','b'}; # @{$hashref{'a','a','b'}} would be dereferencing (as an array) # an element of that arcane structure, a multi-dimensional hash.

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!

Re: slicing a hash ref (quick)
by tye (Sage) on Jul 01, 2006 at 04:10 UTC