Hash array of arrays

I want to generate a hash array with array values from a file. Some example of rows
ATL 1 2 3
BOS 2 3 4 5
DFW 6 5 1 7 8
ATL 1 2

First field is alpha, and the rest are varied length of numeric fields.
The first field is not unique. If duplicate is found, I want to append the numeric fields to the previous values.
So the output would be, for instance

$myHash{ATL}[0] = 1
$myhash{ATL}[4] = 2.

Also, how do I access the results using foreach loop?

Thanks,
pax
cpeters5Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wilcoxonCommented:
use strict;
use warnings;
my $fil = shift or die "Usage: $0 input_file\n";
my %data;
# create the data
open IN, $fil or die "could not read $fil: $!";
while (<IN>) {
    chomp;
    my ($key, @vals) = split /\s+/;
    $data{$key} = [] unless $data{$key};
    push @{$data{$key}}, @vals;
}
close IN;
# access the data
foreach my $key (sort keys %data) {
    # could do something here with each arrayref
    foreach my $val (@{$data{$key}}) {
        # could do something here with each value
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cpeters5Author Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.