Perl wrote:
> 
> Need a little help on summarising 2 different tables into 1, based on a particular 
>field.
> 
> The 2 tables (they are actually content from a log file) look like the follow :
> 
> Table #1
> ------------
> 1006788900 198 36
> 1006788600 29 35
> 1006788300 18 75
> 1006788000 19 65
> 1006787700 42 37
> 1006787400 29 26
> 1006787100 65 84
> 1006786800 6 87
> 
> Table #2
> ------------
> 1006789500 43 47
> 1006789200 23 64
> 1006788900 198 36
> 1006788600 29 35
> 1006788300 18 75
> 1006788000 19 65
> 1006787700 42 37
> 1006787400 29 26
> 
> They may not be of equal length. The first field is TIME, 2nd
> is INPUT and 3rd is OUTPUT. Basically, what I want to do is to
> summarise the table based on the first field (TIME). For example,
> if both tables have TIME as 1006788600, INPUT and OUTPUT from
> both tables whose TIME is 1006788600 will be summed up. Meaning
> the script needs to go thru the tables to look for entries with
> the same TIME, and add the INPUT and OUTPUT together to form
> another summaried table.
> 
> Any easy way for me to do that other than putting the data into
> an array or hash table, and compare them entry by entry??

The easiest way would be to use a hash:

#!/usr/bin/perl -w
use strict;

my %data;
for my $file ( '/data/table1', '/data/table2' ) {
    open IN, "< $file" or die "Cannot open $file: $!";
    while ( <IN> ) {
        my ( $time, $in, $out ) = split;
        $data{ $time }[0] += $in;
        $data{ $time }[1] += $out;
        }
    close IN;
    }

for ( sort keys %data ) {
    print "$_ @{$data{$_}}\n";
    }




John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to