Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6807967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:53:50+00:00 2026-05-26T19:53:50+00:00

I have a question that seems basic, but I can’t figure out. Say that

  • 0

I have a question that seems basic, but I can’t figure out. Say that I have a hash reference in Perl. I want to get an array of values through an array of keys.

Here’s how it’d work with a hash, not a hash reference:

my %testHash = ( "a" => 1, "b" => 2, "c" => 3 );
my @testKeys = ("a", "b", "c");

my @testValues = @testHash{@testKeys};

Now suppose I have a hash reference,

my $hashRef = {"a" => 1, "b" => 2, "c" => 3};

I’ve tried the following two ways:

my @values = @{$hashRef->{@testKeys}};
my @values = $hashRef->{@testKeys};

But neither is correct. Is there a correct way, or do I just have to dereference the hash ref every time I want to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:53:51+00:00Added an answer on May 26, 2026 at 7:53 pm

    You’re close:

    my @values = @$hashref{@testKeys};     ## (1)
    

    or

    my @values = @{$hashref}{@testKeys};   ## (2)
    

    For more examples, see “Slices” in the perllol documentation.

    “Using References” in the perlref documentation gives the general rules.

    1. Anywhere you’d put an identifier (or chain of identifiers) as part of a variable or subroutine name, you can replace the identifier with a simple scalar variable containing a reference of the correct type.

    This explains why (1) works: you replaced the identifier testHash with the simple scalar $hashRef.

    2. Anywhere you’d put an identifier (or chain of identifiers) as part of a variable or subroutine name, you can replace the identifier with a BLOCK returning a reference of the correct type.

    Snippet (2) above is nearly the same but has a little bulkier syntax. In place of the identifier testHash, you write a block returning a reference to a hash, i.e., {$hashRef}.

    The braces here enclose a bona fide block, so you can compute and return a reference, as in

    push @{ $cond ? \@a1 : \@a2 }, "some value";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have another SQL/access 2007 question that seems really basic but I'm not sure
A question that seems to have quite a few options for Python, but none
A bit of a basic question, but one that seems to stump me, nonetheless.
I have probably worded the question badly, but couldn't figure out how to phrase
So I'm learning java, and I have a question. It seems that the types
I have a question that may be quite naive, but I feel the need
I have a question that I'm ashamed to ask, but I'm going to have
This seems to me like a very simple question, but I can't seem to
Another basic Rails question: I have a database table that needs to contain references
Maybe I am wrong, but this seems to be a very basic question. Suddenly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.