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

  • Home
  • SEARCH
  • 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 6333393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:27:12+00:00 2026-05-24T18:27:12+00:00

In Perl to perform a hash update based on arrays of keys and values

  • 0

In Perl to perform a hash update based on arrays of keys and values I can do something like:

@hash{'key1','key2','key3'} = ('val1','val2','val3');

In Ruby I could do something similar in a more complicated way:

hash.merge!(Hash[ *[['key1','key2','key3'],['val1','val2','val3']].transpose ])

OK but I doubt the effectivity of such procedure.

Now I would like to do a more complex assignment in a single line.

Perl example:

(@hash{'key1','key2','key3'}, $key4) = &some_function();

I have no idea if such a thing is possible in some simple Ruby way. Any hints?

For the Perl impaired, @hash{'key1','key2','key3'} = ('a', 'b', 'c') is a hash slice and is a shorthand for something like this:

$hash{'key1'} = 'a';
$hash{'key2'} = 'b';
$hash{'key3'} = 'c';
  • 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-24T18:27:13+00:00Added an answer on May 24, 2026 at 6:27 pm

    You could redefine []= to support this:

    class Hash
      def []=(*args)
        *keys, vals = args # if this doesn't work in your version of ruby, use "keys, vals = args[0...-1], args.last"
        merge! Hash[keys.zip(vals.respond_to?(:each) ? vals : [vals])]
      end
    end
    

    Now use

    myhash[:key1, :key2, :key3] = :val1, :val2, :val3
    # or
    myhash[:key1, :key2, :key3] = some_method_returning_three_values
    # or even
    *myhash[:key1, :key2, :key3], local_var = some_method_returning_four_values
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm quite new to Perl development, and I'd like to perform a following task:
How can I perform a shallow syntax check on perl files. The standard perl
I would like to call other Perl scripts in order to perform a contention
Building off Does Perl have an enumeration type? , how can I perform dynamic
I was wondering how well Perl would perform for OOP based programming. What are
If we can perform this in Perl It would be fine as well. I
Possible Duplicate: How do I perform a Perl substitution on a string while keeping
Perl offers this very nice feature: while ( <> ) { # do something
Using Perl XML::Twig, how can I loop on each sibling until reaching the last
In Perl, you are able to call a function by reference (or name) like

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.