简体   繁体   中英

Sum all values in each(hash of Array) perl

I have a hash of array named %hash

$VAR1 = {
          'reboot' => [
                        4442,
                        3483,
                        541
                      ],
          'prod-dev' => [
                          0,
                          485,
                          3421,
                          242,
                          425,
                          425,
                          484,
                          1,
                          244
                        ]
        };

How to add all the values in each key and print them all at once like this

reboot   : sum
prod-dev : sum

thanks cheers

#!/usr/binkj/perl -w
use strict;
use warnings;
use List::Util qw( sum );
use Data::Dumper;

my ($substr, $new_line);
my @fields;
my %hash =();
open(my kj$logs, ">STDOUT") or die $!;
my ($total_sum, $total_t, $tsum);
my (@array, $key, $val);

while (<STDIN>) {

        my @matches;
        chomp;
        next if $_ =~ m/still logged in/;
        next if $_ =~ m/wtmp/;
        next if $_ =~ m/\(-\d.+?\)/;
        next if $_ =~ m/^$/g;

        $_ =~ /(^.*?)\s.*?(\(.*?\))/g;
       ($key, $val) = ($1,$2);
        $val =~ s/(\(|\))//g;

        if($val =~ m/\d\+.*/){
                $val =~ s/\+/\:/;
                #$val =~ m/(^\d.)(:\d.:)(:\d.\s)/g; 
                my ($days, $hrs, $mins) = $val =~ /^(\d+):(\d+):(\d+).$/g;
                $days = $days * 24 * 60;
                $hrs = $hrs * 60;
                $total_t = $days + $hrs + $mins;
                #print $days . ":" . $hrs . ":" . $mins. "\n";
                print "$total_t \n";
        }else {
                my ($hrs, $mins) = $val =~ /^(\d+):(\d+).$/g;
                $hrs = $hrs * 60;
                $total_t = $hrs + $mins;

                print "$total_t \n";
        }
        push (@{$hash{$key}}, $total_t);
        for my $k (keys(%hash)) {
                printf("%-8s : %d\n", $k,sum( @{ $hash->{$k} } ),);
        }

print Dumper (\%hash);
close $logs;

here is the whole perl program

im really having a hard time solving this one wew I hope you guys can help me

use List::Util qw( sum );

for my $k (keys(%$VAR1)) {
   printf("%-8s : %d\n",
      $k,
      sum( @{ $VAR1->{$k} } ),
   );
}

minor but important difference from above answer in accordance with question and without any external module.

foreach $val (keys(%$VAR1)){
        $s=0;
        foreach ( @{$VAR1->{$val}}){
                $s = $_+$s;
        }
        print "$val :- $s ";

}

I managed to answer it guys, thanks you very much!

foreach my $key ( keys %hash ) {
my $total_sum = 0;              
foreach ( @{$hash{$key}} )  {                   
$total_sum += $_;               
}
print $key . "" . $total_sum . "\n";
}

The function sum from List::Util can be used for the sum:

#!/usr/bin/env perl

use strict;
use warnings;

my %hash = (
    'reboot'   => [ 4442, 3483, 541 ],
    'prod-dev' => [ 0,    485,  3421, 242, 425, 425, 484, 1, 244 ],
);

use List::Util qw( sum );

my @sums;
for my $key ( keys %hash ) {
    my $sum = sum @{ $hash{$key} };
    push @sums, "$key: $sum\n";
}

print @sums;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM