Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

data separation and calculation

Status
Not open for further replies.

xxtravel

Programmer
Aug 24, 2002
22
0
0
MV
i have a file with data as follows;

==============================
Field1 | Field2 | Field3|
==============================
771323 | 02.00 min | 30.00 |
771323 | 04.00 min | 40.00 |
771323 | 05.00 min | 50.00 |
771323 | 06.00 min | 60.00 |
771323 | 07.00 min | 70.00 |
829999 | 02.00 min | 30.00 |
829999 | 04.00 min | 40.00 |
781222 | 05.00 min | 50.00 |
781222 | 06.00 min | 60.00 |
781222 | 07.00 min | 70.00 |
==============================

from the above data, after doing separation and calculation i want an output file with the following data;

==============================
Field1 | Field2 | Field3|
==============================
771323 | 02.00 min | 30.00 |
771323 | 04.00 min | 40.00 |
771323 | 05.00 min | 50.00 |
771323 | 06.00 min | 60.00 |
771323 | 07.00 min | 70.00 |
----------------------------
Total $ 250 $

829999 | 02.00 min | 30.00 |
829999 | 04.00 min | 40.00 |
----------------------------
Total $ 70 $

781222 | 05.00 min | 50.00 |
781222 | 06.00 min | 60.00 |
781222 | 07.00 min | 70.00 |
----------------------------
Total $ 180 $
==============================

it was difficult for me to explain this in written, i hope you could understand this and explain me how i can proceed.

chao, xtravel



My Site:
 
Assuming the size of the file is not too large, and the order is not that important:
Code:
@lines = grep { /min/ } <DATA>;

for (@lines) {
    @f = split(/\|/, $_);
    $sum{$f[0]} += $f[2];
    push @{$lines{$f[0]}}, $_;
}

foreach (sort keys %lines) {
    print @{$lines{$_}};
    print "Sum: $sum{$_}\n";
}

__DATA__
==============================
Field1 |  Field2   | Field3|
==============================
771323 | 02.00 min | 30.00 |
771323 | 04.00 min | 40.00 |
771323 | 05.00 min | 50.00 |
771323 | 06.00 min | 60.00 |
771323 | 07.00 min | 70.00 |
829999 | 02.00 min | 30.00 |
829999 | 04.00 min | 40.00 |
781222 | 05.00 min | 50.00 |
781222 | 06.00 min | 60.00 |
781222 | 07.00 min | 70.00 |
==============================
Will produce:
Code:
771323 | 02.00 min | 30.00 |
771323 | 04.00 min | 40.00 |
771323 | 05.00 min | 50.00 |
771323 | 06.00 min | 60.00 |
771323 | 07.00 min | 70.00 |
Sum: 250
781222 | 05.00 min | 50.00 |
781222 | 06.00 min | 60.00 |
781222 | 07.00 min | 70.00 |
Sum: 180
829999 | 02.00 min | 30.00 |
829999 | 04.00 min | 40.00 |
Sum: 70
Cheers, Neil
 
If the order is important, and the file size is too large, then perhaps:
Code:
$cursor = -1;
$sum = 0;

while (<DATA>) {
    next unless /min/;
    @f = split(/\|/, $_);
    if ($cursor == -1) { $cursor = $f[0] }
    if ($cursor != $f[0]) {
        print "Sum: $sum\n";
        $sum = 0;
        $cursor = $f[0];
    }
    $sum += $f[2];
    print;
}
print "Sum: $sum\n";
Cheers, Neil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top