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 9139221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:21:34+00:00 2026-06-17T09:21:34+00:00

Purely academic question, and I don’t see instructions banning them here ( although there

  • 0

Purely academic question, and I don’t see instructions banning them here (although there is no ‘academic’-like tag I could find).

If I have an existing hash like the following, I can take a slice(?) of it as shown:

my %hash = (one=>1, two=>2, three=>3, four=>4);
my ($two, $four) = @hash{'two','four'};

Is there a way to do this if the hash is returned from an example function like this?

sub get_number_text
{
    my %hash = (one=>1, two=>2, three=>3, four=>4);
    return %hash;
}

One way that works is:

my ($two, $four) = @{ { get_number_text() } }{'two', 'four'};

As I understand it, function returns a list of hash keys/values, the inner {} creates an anonymous hash/ref, and @{} uses the reference to “cast” it to a list aka a hash slice since Perl knows the ref is a hash. (I was a little surprised that the last bit worked, but more power to Perl, etc.)

But is that the clearest way to write that admittedly strange access in one expression?

  • 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-06-17T09:21:34+00:00Added an answer on June 17, 2026 at 9:21 am

    In general, avoid returning a flattened hash (return %foo) from a subroutine; it makes it harder to work with without copying it into another hash. Better to return a hash reference (return \%foo).

    But yes, that is the clearest way. Though often lists of hardcoded keys are given using qw:

    my ($two, $four) = @{ { returnit() } }{ qw/two four/ };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are data structures like linked lists something that are purely academic for real programming
This is a question purely to satisfy my own curiosity. Here in Norway it's
I have a purely academic question about SQLite databases. I am using SQLite.net to
This is a purely academic question, but what's the difference between using == and
So this is a purely academic question, mostly as its been a while since
This question is purely academic, because I'd never dream of doing this in real
The problem is purely academic, so the following is just an example; if I
This is a purely theoretical 'what if' question, so no pressure to rush to
A Purely theoretical question on Tcl. Following this question I was thinking on what
As a purely academic exercise, I'm writing a recursive descent parser from scratch --

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.