Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Fetch array of values from hashref

by LanX (Saint)
on Sep 10, 2019 at 16:28 UTC ( [id://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

    demo:

    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?
    Username:
    Password:

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

    How do I use this?Last hourOther CB clients
    Other Users?
    Others having a coffee break in the Monastery: (4)
    As of 2024-04-19 05:21 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found