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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:51:39+00:00 2026-05-17T15:51:39+00:00

I have an array of hashes, all with the same set of keys, e.g.:

  • 0

I have an array of hashes, all with the same set of keys, e.g.:

my $aoa= [
 {NAME=>'Dave', AGE=>12, SEX=>'M', ID=>123456, NATIONALITY=>'Swedish'},
 {NAME=>'Susan', AGE=>36, SEX=>'F', ID=>543210, NATIONALITY=>'Swedish'},
 {NAME=>'Bart', AGE=>120, SEX=>'M', ID=>987654, NATIONALITY=>'British'},
]

I would like to write a subroutine that will convert this into a hash of hashes using a given key hierarchy:

my $key_hierarchy_a = ['SEX', 'NATIONALITY'];
aoh_to_hoh ($aoa, $key_hierarchy_a) = @_;
 ...
}

will return

{M=>
  {Swedish=>{{NAME=>'Dave', AGE=>12, ID=>123456}},
   British=>{{NAME=>'Bart', AGE=>120, ID=>987654}}}, 
 F=>
  {Swedish=>{{NAME=>'Susan', AGE=>36,  ID=>543210}}
}

Note this not only creates the correct key hierarchy but also remove the now redundant keys.

I’m getting stuck at the point where I need to create the new, most inner hash in its correct hierarchical location.

The problem is I don’t know the “depth” (i.e. the number of keys). If I has a constant number, I could do something like:

%h{$inner_hash{$PRIMARY_KEY}}{$inner_hash{$SECONDARY_KEY}}{...} = filter_copy($inner_hash,[$PRIMARY_KEY,$SECONDARY_KEY])

so perhaps I can write a loop that will add one level at a time, remove that key from the hash, than add the remaining hash to the “current” location, but it’s a bit cumbersome and also I’m not sure how to keep a ‘location’ in a hash of hashes…

  • 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-17T15:51:40+00:00Added an answer on May 17, 2026 at 3:51 pm
    use Data::Dumper;
    
    my $aoa= [
     {NAME=>'Dave', AGE=>12, SEX=>'M', ID=>123456, NATIONALITY=>'Swedish'},
     {NAME=>'Susan', AGE=>36, SEX=>'F', ID=>543210, NATIONALITY=>'Swedish'},
     {NAME=>'Bart', AGE=>120, SEX=>'M', ID=>987654, NATIONALITY=>'British'},
    ];
    
    sub aoh_to_hoh {
      my ($aoa, $key_hierarchy_a) = @_;
      my $result = {};
      my $last_key = $key_hierarchy_a->[-1];
      foreach my $orig_element (@$aoa) {
        my $cur = $result;
        # song and dance to clone an element
        my %element = %$orig_element;
        foreach my $key (@$key_hierarchy_a) {
          my $value = delete $element{$key};
          if ($key eq $last_key) {
            $cur->{$value} ||= [];
            push @{$cur->{$value}}, \%element;
          } else {
            $cur->{$value} ||= {};
            $cur = $cur->{$value};
          }
        }
      }
      return $result;
    }
    
    my $key_hierarchy_a = ['SEX', 'NATIONALITY'];
    print Dumper(aoh_to_hoh($aoa, $key_hierarchy_a));
    

    As per @FM’s comment, you really want an extra array level in there.

    The output:

    $VAR1 = {
              'F' => {
                       'Swedish' => [
                                      {
                                        'ID' => 543210,
                                        'NAME' => 'Susan',
                                        'AGE' => 36
                                      }
                                    ]
                     },
              'M' => {
                       'British' => [
                                      {
                                        'ID' => 987654,
                                        'NAME' => 'Bart',
                                        'AGE' => 120
                                      }
                                    ],
                       'Swedish' => [
                                      {
                                        'ID' => 123456,
                                        'NAME' => 'Dave',
                                        'AGE' => 12
                                      }
                                    ]
                     }
            };
    

    EDIT: Oh, BTW – if anyone knows how to elegantly clone contents of a reference, please teach. Thanks!

    EDIT EDIT: @FM helped. All better now 😀

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

Sidebar

Related Questions

I have this array of hashes: - :name: Ben :age: 18 - :name: David
I have the following array of hashes, and want to remove the ones that
Say I have an array of objects that I want to be able to
I have a snippet of code im trying to parse with nokogiri that looks
I am basically a networking guy and so not good at writing scripts. While
hi I am getting data from a database to create some graphs. I am
I need to find identical sequences of characters in a collection of texts. Think
The actual problem I was originally doing was converting a hash of arrays into
This may be a really long stretch but would make life a fair bit
What is the simplest way to convert the range 1..10 into a hash of

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.