snape has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks,
I am having printing error while I am trying to print the Hash of Array. I have an input file as follows:
id col1 col2 col3 12 100 12 196 12 120 15 190 13 90 190 200 13 70 20 20 13 101 340 25 14 100 123 19 15 80 389 39
I have done the sum of the rows without taking the the values of the same id present in other columns of the file. I try to insert the sum of row values as a first key into another hash and id as the second key. The column values for that id is the array for that hash. My code is below and having run time errors. Also, when I use Data Dumper it gives me correct output. I would like to know what and where is my mistake in the program.
Thanks.sub sort { my ($INPUT, $OUT) = @_; my %data; while (<$INPUT>) { next if (/^I|A|M/); chomp; my ($id,@cols) = split ('\t',$_); push @{$data{$id}}, \@cols; } my %hash; for my $key (keys %data){ for my $aref(@{$data{$key}}){ my $sum = sum(@{$aref}); print $sum,"\n"; push (@{$hash{$sum}{$key}}, @{$aref}); } } for my $sum (sort keys %hash){ for my $key({$hash{$sum}}){ for my $aref (@{$hash{$sum}{$key}}){ print "$key\t$sum\t"; } } } print $OUT Dumper(\%hash); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Printing Error of Hash of Array
by Corion (Patriarch) on Feb 01, 2010 at 08:23 UTC | |
by snape (Pilgrim) on Feb 01, 2010 at 14:52 UTC | |
by Corion (Patriarch) on Feb 01, 2010 at 15:04 UTC | |
by Anonymous Monk on Feb 01, 2010 at 15:16 UTC | |
by snape (Pilgrim) on Feb 01, 2010 at 16:27 UTC |