Addition of same value

R

Rita

Hi Members ,
I have some values like
120.00 14.85
125.00 12.53
125.00 10.576
125.00 15.326
127.00 18.245
127.00 5.243
129.00 10.253
132.00 15.02
135.00 12.356
135.00 12.34
135.00 14.35
137.00 11.21
.. .
.. .
.. .
I want to make programm Which find all the same values from left side
and do addition of that value of right side.
Like i had 3 times 125.00 so i want output 12.53+10.576+15.326
So what will be code for it?
Thanks
 
J

Jürgen Exner

Rita said:
Hi Members ,

What about those in this NG without a member?
I have some values like
120.00 14.85
125.00 12.53
125.00 10.576
125.00 15.326
127.00 18.245
127.00 5.243
129.00 10.253
132.00 15.02
135.00 12.356
135.00 12.34
135.00 14.35
137.00 11.21
. .
. .
. .
I want to make programm Which find all the same values from left side
and do addition of that value of right side.
Like i had 3 times 125.00 so i want output 12.53+10.576+15.326

Simple. Whenever you hear "same" then a hash is a likely candidate for a
solution.

Just use the left number as a key and as you walk through the values add the
right number to the value.
So what will be code for it?

What do you have so far?

jue
 
T

Tad McClellan

I want to make programm Which find all the same values from left side
and do addition of that value of right side.
So what will be code for it?


------------------------------
#!/usr/bin/perl
use warnings;
use strict;

my %nums;
while ( <DATA> ) {
my($k, $v) = split;
$nums{$k} += $v;
}

foreach ( sort {$a <=> $b} keys %nums ) {
print "$_ sums to $nums{$_}\n";
}

__DATA__
120.00 14.85
125.00 12.53
125.00 10.576
125.00 15.326
127.00 18.245
127.00 5.243
129.00 10.253
132.00 15.02
135.00 12.356
135.00 12.34
135.00 14.35
137.00 11.21
 
R

Rita

Tad said:
------------------------------
#!/usr/bin/perl
use warnings;
use strict;

my %nums;
while ( <DATA> ) {
my($k, $v) = split;
$nums{$k} += $v;
}

foreach ( sort {$a <=> $b} keys %nums ) {
print "$_ sums to $nums{$_}\n";
}

__DATA__
120.00 14.85
125.00 12.53
125.00 10.576
125.00 15.326
127.00 18.245
127.00 5.243
129.00 10.253
132.00 15.02
135.00 12.356
135.00 12.34
135.00 14.35
137.00 11.21
Thanks.
 
B

Brad Baxter

Rita said:
Hi Members ,
I have some values like
120.00 14.85
125.00 12.53
125.00 10.576
125.00 15.326
127.00 18.245
127.00 5.243
129.00 10.253
132.00 15.02
135.00 12.356
135.00 12.34
135.00 14.35
137.00 11.21
. .
. .
. .
I want to make programm Which find all the same values from left side
and do addition of that value of right side.
Like i had 3 times 125.00 so i want output 12.53+10.576+15.326
So what will be code for it?
Thanks

perl -pale'$_{$F[0]}+=$F[1]}{$_="@{[%_]}"' textfile

or more transparently:

perl -nale '$h{$F[0]}+=$F[1]; END {print"$_ $h{$_}" for keys %h}'
textfile
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top