Here is one shot at it:
#!perl -w
my %Quantities = ();
while ( <DATA> ) {
my ($partnum,$qty) = split(/_/, $_);
if ( ! defined $Quantities{$partnum} ) {
$Quantities{$partnum} = 0;
}
$Quantities{$partnum}+=$qty;
}
foreach (sort keys %Quantities ) {
printf "%-6s: %6d\n", $_, $Quantities{$_};
}
__DATA__
a8160_1234
a8161_5678
a8162_9876
a8163_4321
a8164_8765
a8165_3000
a8165_1800
^--- Script ends here
Output:
a8160 : 1234
a8161 : 5678
a8162 : 9876
a8163 : 4321
a8164 : 8765
a8165 : 4800
Wags ;)
-----Original Message-----
From: C.E.O. [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, November 27, 2001 12:19
To: [EMAIL PROTECTED]
Subject: Need help with data file ...
I'm having trouble trying to add some values. I have a data file that
consists of part numbers and quantities. I open that file, load the
data file into an array. My problem is that my data file has been
created by a process I cannot control, so that many times the
data file will show the same part number but with a different
quantity.
I need to add all of the quantities that are shown with the same
part number. Here is some sample code and the data file ...
Any help is appreciated,
~
#!/usr/bin/perl
print "Content-type: text/html\n\n";
open(INF,"expr3.dat") or print "Can't open expr3.dat: $!";
@partandqty = <INF>;
close(INF);
foreach $partandqty (@partandqty) {
($partnum,$qty) = split(/_/, $partandqty);
print "$partnum ** $qty<br>";
###### IF THE PARTNUM IS THE SAME - ADD THE QUANTITIES
OF THE SAME PARTNUM
###
}
+++
a8160_1234
a8161_5678
a8162_9876
a8163_4321
a8164_8765
a8165_3000
a8165_1800
--
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]