in reply to Hash of Arrays and File Operations
I would like to know how can I do the sum of the rows without taking the the values of the same id present in other columns of the file.
prints:use strict; use warnings; use List::Util qw(sum); my %data; while (<DATA>) { next if /^id/; my ($id, @cols) = split; push @{ $data{$id} }, \@cols; } for my $id (sort keys %data) { for my $aref (@{ $data{$id} }) { print "$id ", sum(@{$aref}), "\n"; } } __DATA__ 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
12 308 12 325 13 480 13 110 13 466 14 242 15 508
Update:
1. I would like to know that how will I do the translation/ substitution operation on the data stored in Hash of Array data structures.In general, you will loop through your data structure using for, but, depending on exactly what your need to modify, you might be able to use map. Of course, if possible, it would be best to modify before you load it into your data structure in the first place.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Hash of Arrays and File Operations
by snape (Pilgrim) on Jan 28, 2010 at 19:20 UTC | |
by toolic (Bishop) on Jan 28, 2010 at 20:15 UTC | |
by Anonymous Monk on Jan 28, 2010 at 20:24 UTC | |
by snape (Pilgrim) on Jan 28, 2010 at 22:38 UTC | |
by Anonymous Monk on Jan 28, 2010 at 22:56 UTC | |
|