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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:15:33+00:00 2026-06-04T16:15:33+00:00

I have the following hash my %input_hash = ( ‘test1’ => ‘100’, ‘test2’ =>

  • 0

I have the following hash

my  %input_hash = (
    'test1' => '100',
    'test2' => '200',
    'test3' => '300',
    'test4' => '400',
    'test5' => '500'
);

What I need is to build a hash of hash from the above hash. I need to put the first 2 of the above key value pair into a key of the hash of hash. Better explained with this example.
Desired output:

my %expected_hash = (
1 => {
    'test1' => '100',
    'test2' => '200',

},
2 => {
    'test3' => '300',
    'test4' => '400',
},
3 => {
    'test5' => '500'
},

);

I would like the split to be dynamic. Example,if i need to split by 3, the desired output should be

my %expected_hash = (
1 => {
    'test1' => '100',
    'test2' => '200',
    'test3' => '300',
},
2 => {
    'test4' => '400',
    'test5' => '500'
},

);

  • 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-04T16:15:35+00:00Added an answer on June 4, 2026 at 4:15 pm

    Here’s a solution using an index array built from number of keys in %input_hash and desired size set in $chunk_size:

    #!/usr/bin/env perl
    use strict;
    use warnings;
    use Data::Dumper;
    
    my  %input_hash = (
        'test1' => '100',
        'test2' => '200',
        'test3' => '300',
        'test4' => '400',
        'test5' => '500'
    );
    
    my $chunk_size = 2;
    my @indexes = map {int($_ / $chunk_size) + 1} 0 .. keys %input_hash;
    my %expected_hash;
    
    for my $key (sort keys %input_hash) {
        my $index = shift @indexes;
        $expected_hash{$index}{$key} = $input_hash{$key};
    }
    
    print Dumper \%expected_hash;
    

    Output:

    $VAR1 = {
          '1' => {
                   'test1' => '100',
                   'test2' => '200'
                 },
          '3' => {
                   'test5' => '500'
                 },
          '2' => {
                   'test4' => '400',
                   'test3' => '300'
                 }
        };
    

    Of course, as TLP mentioned, you have to sort %input_hash to achieve this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say you have the following Ruby hash, hash = {:a => [[1, 100..300], [2,
I have the following hash of countries; COUNTRIES = { 'Albania' => 'AL', 'Austria'
According to MSDN , a hash function must have the following properties: If two
I have a hash table with the following contents: my %hash = ( 'a'
I'm trying to do the following say; I have a hash: {word1 => {doc1
Can you please help anyone?. I have a hash value as following. I need
Let's say I have the following hash: { :foo => 'bar', :baz => 'qux'
In rails 3, how to create a Dropdown from hash I have following code
I have the following hash function, and I'm trying to get my way to
I have the following hash: { groups => [ { type => Nearby, venues

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.