It depends on how you have set-up your arrays. If they are like this: index @ar_1 @ar2
0 John London
1 Mike Paris
2 Vivien Milan
3 John Berlin
4 Vivien Paris
etc...
Then you would like to end up with a Hash of Array structure as follows:
John => [London, Berlin],
Mike => [Paris],
Vivien => [Milan, Paris]
Which you can do as follows:
use strict;
use Data::Dump qw/dump/;
my @ar_1 = qw/John Mike Vivien John Vivien/;
my @ar_2 = qw/London Paris Milan Berlin Paris/;
my %hash;
foreach (0 .. $#ar_1) {
push @{$hash{$ar_1[$_]}}, $ar_2[$_];
}
print dump(\%hash);
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
|