简体   繁体   English

对每个(数组哈希)perl中的所有值求和

[英]Sum all values in each(hash of Array) perl

I have a hash of array named %hash 我有一个名为%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 这是整个perl程序

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: List::Util的函数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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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