CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
data separation and calculation

 



xtravel
New User

Oct 16, 2004, 1:44 AM

Post #1 of 2 (909 views)
data separation and calculation Can't Post

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


KevinR
Veteran


Oct 16, 2004, 11:33 PM

Post #2 of 2 (906 views)
Re: [xtravel] data separation and calculation [In reply to] Can't Post

something along these lines:


Code
#!/usr/bin/perl 
use strict;
print "Content-type: text/html\n\n";

my %hash = ();

while (<DATA>) {
chomp;
my ($id,$mins,$qty) = split(/\|/);
$hash{$id}+=$qty;
}

foreach my $keys (sort keys %hash) {
print "$keys = $hash{$keys}<br>\n";
}

__DATA__
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 |


your data is probably in a text file so open the file and either read the data into an array or loop through it using a "while" loop as in the above example.
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives