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.
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
prints:
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.

In reply to Re: Hash of Arrays and File Operations by toolic
in thread Hash of Arrays and File Operations by snape

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.