Hi all,
Thanks for those who replied to my posting.... appreciate all the help
given. :)
Based on the code written by John (thanks a lot), I would like to ask a
further question :
John's code ==>
#!/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";
}
The sample 2 tables (fields being TIME, IN, OUT respectively) ==>
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
Is there a way to modify the above code to summarise ONLY common values
based on TIME field?? For example, the 1st record (TIME field) in Table #2
does not appear anywhere in Table #1, so the output shouldn't contain this
record. In other words, the output from the code should only display records
whereby TIME (as a key) appears in BOTH tables, not just one.
I hope I am clear enough in my elaboration.
Thanks guys. :)
----- Original Message -----
From: "John W. Krahn" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, December 14, 2001 5:49 PM
Subject: Re: Summarising tables
> 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]
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]