PDA

View Full Version : hash and arrar references


shyam
08-05-2005, 03:47 PM
hi,
i've a file that contains a delimited list of numbers like this

1,2
1,3
1,1
2,1
2,4
3,1
4,1
5,2
...

i want to construct a hash which should look like this
%target = (1=>(2,3,1), 2=>(1,4), 3=>(1), 4=>(1), 5=>(2));

being new to perl i'm clueless to the more advanced array/hash referencing :( any help will be appreciated

FishMonger
08-05-2005, 08:59 PM
#!/usr/bin/perl -w

use strict;
use Data::Dumper;

my %target;
while(<DATA>) {
chomp;
my ($num, $value) = split ',';
push @{$target{$num}}, $value;
}
print Dumper %target;

__DATA__
1,2
1,3
1,1
2,1
2,4
3,1
4,1
5,2

shyam
08-07-2005, 12:06 PM
wow! thanx FishMonger :) Ur the best :D