Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

slicing a hash ref

by rvosa (Curate)
on Jul 01, 2006 at 02:41 UTC ( [id://558719]=perlquestion: print w/replies, xml ) Need Help??

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://558719]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-24 05:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found