On Friday 14 December 2001 04:39, you wrote:
> Hi all,
>
> 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.

I cannot help you with funky perl... But is a classic programming problem 
from the days of tape streamers and COBOL.  It is called a `merge' , 
suprisingly.  This is a very efficient way to merge large amounts of data. 
The only gotcha is that the files must be sorted on the merge key (ie TIME), 
which I am happily asuming since it is a timestamp.  There might be the odd 
ocasion that the input files are not sorted( Machine bounced and the BIOS 
clock resets itself). Putting a sorted file thru a quick sort uses virtually 
no resources, and then you will be garanteed to get the correct results.

This is a lot more typing but classic algorithms are such joy to code.. (For 
me anyway);

Pseudo code:

open file1, file2

(time1, input1, output) = read file1
(time2, input2, output2) = read file1

while not end of file file1 and file2 {
    if (time1 == time2 {
       input = input1 + input2
       readboth = true;
    } 
    if (time1 < time2) {
        input = input1
        read1 = true;
    }
    if (time2 < time1) {
        input = input2
        read2 = true;
    }
       write summary with input
    
    if (read1 || readboth ) {
       if not eof file1 then (time1, input1, output) = read file1
    };
    if (read2 || readboth ) {
      if not eof file2 (time2, input2, output2) = read file1
    }
    readboth, read1, read2 = false
}


Regards
Johan

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

Reply via email to