# Hash Arithmetic

Greetings Experts,

This is not a homework assignment.
I have a hash that has the following format
17asb345 1
17asb345 2
17asb345 5
17asb345 3
82dsic456 1
82dsic456 9
82dsic456 3
34xsw567 20
34xsw567 2
34xsw567 5

I want to calculate the net sum for the lines that start with the same string.

I have written the following, but would like code that is more efficient.

==begin code.pl
#!/usr/bin/perl

use strict;

my @newkeyArray;
my \$lkey;
my \$compkey;
my \$lcompkeyvalue;
my \$compkeyvalue;
my \$lkeyvalue;

foreach my \$newkeyArray (@newkeyArray) {
my (\$fkey,\$fkeyvalue) = split /\s/,\$newkeyArray;
\$compkey = \$fkey;
\$compkeyvalue = \$fkeyvalue;
if (\$fkey eq \$compkey) {
if (\$fkeyvalue > \$compkeyvalue) {
print "The \$fkeyvalue is greater >= \$compkeyvalue\n";
}
\$lcompkeyvalue = \$compkeyvalue;
}
\$compkeyvalue = \$fkeyvalue;
print "The first key is: \$fkey\n";
print "\$fkeyvalue\n";
\$lkey = \$compkey;
}
==end code.pl

###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
my @newkeyArray;
my %key_sum;
my %key_count;

map({my (\$k,\$v)=split(/\s/);\$key_sum{\$k}+=\$v;\$key_count{\$k}++} @newkeyArray);

for (keys(%key_count)){
print "key=\$_ sum=\$key_sum{\$_}\n" if(\$key_count{\$_} > 1);
}
0
Commented:
foreach (@newKeyArray) {
\$sums{\$1} += \$2 if /^(\w+)\s+(\d+)/;
}
0
Author Commented:
rugdog, if the has has odd elements such as this:
17asb345 1
17asb345 2
17asb345 5
17asb345 3
82dsic456 1
82dsic456 9
82dsic456 3
34xsw567 20
34xsw567 2
34xsw567 5
52wox678 10

The script does not yield a value for 52wox678.  How do you account for that?
0
Commented:
well since you asked the script to print the sum of  lines with the same key, the one I wrote, will print only the keys that are repeated, to print all keys sums:

my @newkeyArray;
my %key_sum;

map({my (\$k,\$v)=split(/\s/);\$key_sum{\$k}+=\$v;} @newkeyArray);

for (keys(%key_sum)){
print "key=\$_ sum=\$key_sum{\$_}\n";
}
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
rugdog, sorry for changing directions on you.  The change works great.  Thank you.
0
Commented:
you're welcome
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.