Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Fetch array of values from hashref

by LanX (Cardinal)
on Sep 10, 2019 at 16:28 UTC ( #11105974=note: print w/replies, xml ) Need Help??

in reply to Fetch array of values from hashref

Rule of thumb for Perl:

  • Replace the symbol with the ref!
  • Respect precedence
  • h is the symbol in %h
  • $h_ref is the ref


    DB<1> @a = c..d DB<2> @h{a..d}=1..4 DB<3> $h_ref = \%h DB<4> x $h_ref # dump all 0 HASH(0x3531868) 'a' => 1 'b' => 2 'c' => 3 'd' => 4 DB<5> x @h{@a} 0 3 1 4 DB<6> x @{$h_ref}{@a} # play it safe 0 3 1 4 DB<7> x @$h_ref{@a} # brackets not needed b/c precedence already r +espected 0 3 1 4 DB<8>

    HTH! :)

    PS: Side note: "hashslice from hashref" is the technical term for "Fetch array of values from hashref"

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

  • Log In?

    What's my password?
    Create A New User
    Node Status?
    node history
    Node Type: note [id://11105974]
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (4)
    As of 2020-10-22 08:47 GMT
    Find Nodes?
      Voting Booth?
      My favourite web site is:

      Results (225 votes). Check out past polls.