[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 397
  • Last Modified:

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.

Thanks in advance for your help.

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

0
uluttrell
Asked:
uluttrell
  • 3
  • 2
1 Solution
 
rugdogCommented:
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
 
kanduraCommented:
foreach (@newKeyArray) {
    $sums{$1} += $2 if /^(\w+)\s+(\d+)/;
}
0
 
uluttrellAuthor 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
rugdogCommented:
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
 
uluttrellAuthor Commented:
rugdog, sorry for changing directions on you.  The change works great.  Thank you.
0
 
rugdogCommented:
you're welcome
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now