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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:50:51+00:00 2026-05-23T18:50:51+00:00

Explanation One array holds a set of keys. The values to these keys are

  • 0

Explanation

One array holds a set of keys. The values to these keys are inner hashes. The keys of these inner hashes, in this case are numbers (like array indices). Another array holds the values of the inner hash.

Question:

How can you populate the outer hash keys with the correct corresponding values (ie. correct inner hash)?

Contraints

I’d prefer a solution utilizing slices, map or grep. Eliminating cascading for loops

I realize it should be an HoA. But this is only for me to learn, it has no functional value…

Working Code:

This code works as I want but I would like to use more advanced techniques:

#! usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my %register=();

my @classNames = ('Science_class', 'Math_class');
my @Science_class_student_names = ('George', 'Lisa', 'Mathias'); #prob from file
my @Math_class_student_names = ('Martin', 'Anna', 'Peter', 'George'); #prob from file

foreach my $className (@classNames) {
    my $array_name = $className.'_'.'student_names';
    if ($array_name =~ /Science/) {
        foreach (0..$#Science_class_student_names ) {
            $register{$className}{$_ + 1} = $Science_class_student_names[$_];
        }
    }
    elsif ($array_name =~ /Math/) {
        foreach (0..$#Math_class_student_names ) {
            $register{$className}{$_ + 1} = $Math_class_student_names[$_];
        }
    }
}

print Dumper(\%register);

Ideas

A hash slice works for direct key-value pairs, but the intermediate keys are throwing me off. Trying something like: @register{@classNames} = map{$count => $student}

One idea I had, before the if statements was if there was a way to use a string in the name of an array: $#($array_name)student_names but that doesn’t work.

Another would be to separately create an array of all the inner hash keys, use a slice and then put that hash into the outer hash.

The only other idea I had was using an AoA to hold all the ‘inner hash value’ arrays. (ie. my @studentNames = (\@Science_class_student_names, \@Math_class_student_names); but haven’t gotten anywhere with that yet.

  • 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-23T18:50:52+00:00Added an answer on May 23, 2026 at 6:50 pm

    It’s easier to work on it a layer at a time. For the inner layer, it seems you want

    1 => George,
    2 => Lisa,
    ...
    

    So start by figuring out how to do that.

    map { $_+1 => $Science_class_student_names[$_] }
       0..$#Science_class_student_names
    

    So you end up with

    $register{'Science_class'} = {
       map { $_+1 => $Science_class_student_names[$_] }
          0..$#Science_class_student_names
       }
    };
    
    $register{'Math_class'} = {
       map { $_+1 => $Math_class_student_names[$_] }
          0..$#Math_class_student_names
       }
    };
    

    If you generalise the inner layer, you get

    for (
       [ 'Science_class' => \@Science_class_student_names ],
       [ 'Math_class'    => \@Math_class_student_names ],
    ) {
       my ($class_name, $student_names) = @$_;
       $register{$class_name} = {
          map { $_+1 => $student_names->[$_] }
             0..$#$student_names
       };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a simple multidimensional structure, like this one: somestr<-array(sample.int(2, 120, replace=TRUE), dim=c(4,5,6))
Can anyone please tell why this code doesn't work: $ser = array('one','two','three','four'); $x =
I want to retrieve from the database an array of values, one for each
The manual on extract shows you can extract an array like: extract(array('one'=>1,'two'=>2)); into $one,$two...
Why is only one value of the db checkbox values array being sent to
This is my function def rating(array) sum_count = array.values.inject(0) { |sum_count,value| sum_count + value
Quick Explanation One Silverlight (3.0) project with several XAML pages. I want to load
what is the proper way to scale an SDL Surface? I found one explanation
I am looking for a good explanation of why one would use sliding expiration
Could any one give an explanation on how a DHT works? Nothing too heavy,

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.