On 3/22/11 Tue  Mar 22, 2011  2:24 PM, "Chris Stinemetz"
<cstinem...@cricketcommunications.com> scribbled:

>>> No, it doesn't. What is a "rlptxat" element? Where do they come from.
> 
> "rlptxat" is just an element indexed at 44 that has a value, in which I would
> like to sum up, when it has the same elements "cell" "sect" and "carr" in the
> record.

OK. With this information and that from previous posts, your requirements
may be summaryized as follows:

You have a file with one record per line, each line consisting of a number
of fields. Within each record may be found values of cell, sect, carr, and
rlptxat at fixed column positions (e.g. 44 for rlptxat). You with to sum up
the values of the rlptxat field for all records having the same values of
cell, sect, and carr.

Correct?

> At 8:03 PM -0600 3/20/11, Chris Stinemetz wrote:
>> Jim,
>> 
>> Thanks for your feedback. I am actually trying to sum up all rlptxat
>> elements that have the same cell, sect, and chan elements in the
>> array. I hope this clarifies.
> 
> No, it doesn't. What is a "rlptxat" element? Where do they come from.
> 
> You can define a multiple-level hash with three nested keys (cell,
> sect, chan) to hold the sum of all elements with the same key values
> (a triple):
> 
>    my %sum;
> 
> Find values of cell, sect, and chan:

Let @record be the array containing the values from each row, as returned by
the split function, for example. Then:

 
my $cell = $record[?] # ? is column number for cell field
my $sect = $record[?] # ? is column number for sect field
my $chan = $record[?] # ? is column number for chan field
my $rlptxat = $record[44];

Or more succintly:

my( $cell, $sect, $chan, $rlptxat ) = @record[ ?, ?, ?, 44 ]; # ? as above

> 
> Add the value with these keys to the sum:
> 
    $sum{$cell}{$sect}{$chan} += $rlptxat

> 
> Iterate over the result:
> 

   for my $cell ( sort keys %sum ) {
     for my $sect ( sort keys %{$sum{$cell}} ) {
       for my $chan ( sort keys %{$sum{$cell}{$sect}} ) {
           print "Value of sum($cell,$sect,$chan) is " .
             $sum{$cell}{$sect}{$chan} . "\n";
       }
     }
   }

And you are done.



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to