We help IT Professionals succeed at work.

In Perl, is it possible to map two arrays of different length into one hash?

akatsuki27 asked
Last Modified: 2017-08-05
I want to know if it's possible to map two arrays of different size into one hash. One array (arr1) has a list of directories and the other (arr2) has a list of 30 files under each directory.  The size of arr1 is 122 elements and the size of arr2 is 3660 elements.  Since keys have to be unique, the keys would be the files and the values would be the directories.  Below are examples of how the arrays are populated and the possible hash from combining them:

arr1 = (dir1, dir2, dir3, ... , dir122 );
arr2 = (dir1_file1, dir1_file2, dir1_file3, ... , dir1_file30, dir2_file1, dir2_file2, dir2_file3, ... ,dir2_file30, dir3_file1,.... etc );

%hash = (

dir1_file1 => dir1
dir1_file2 => dir1
dir1_file3 => dir1
dir1_file30 => dir1
dir2_file1 => dir2
dir2_file2 => dir2
dir2_file3 => dir2
dir2_file30 => dir2
dir3_file1 => dir3
dir3_file2 => dir3
dir3_file3 => dir3
dir3_file30 => dir3
dir4_file1 => dir4
dir122_file30 => dir122

Open in new window

No idea how to code this as I'm weak with hashes so any help is appreciated. Thanks in advance!
Watch Question


I tried this bit of code that I found online but it doesn't populate well. Some fields are done and the majority print out 'undef'.

@arr_map{@keys} = @values;

Open in new window

Unlock this solution and get a sample of our free trial.
(No credit card required)


Thanks for the input, Wilcoxon.

Your code is almost working, I'm now able to map value to each key but it's not sorted the way I thought it would come out.  I'm getting random directories for each file.

So I checked the array of files and I noticed that I'm printing out the files in the following order:


As a result, I either have to sort the files or map the directory to each 1st, 30th, 60th files in order to get the correct key value pair.  Which of those is the right way to solve this?
Unlock this solution and get a sample of our free trial.
(No credit card required)

I was the only one to respond to the question.  The first solution worked exactly as requested in the OP (and almost as desired with the changes).  The second solution gave something close to what the revised requirements wanted.  The author never replied after that.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.