in reply to Re^6: Turning regex capture group variables into arrays, then counting the number of objects in the array
in thread Turning regex capture group variables into arrays, then counting the number of objects in the array

Here's one approach which looks complex but you can break it down into its constituent parts easily enough.

#!/usr/bin/env perl use strict; use warnings; my %counts = ( Status => { 0 => 99, 1 => 12, 2 => 3, 77 => 4 } # Other keys would go here ); my $totalfails = 0; $totalfails += $counts{Status}{$_} for grep {$_ > 1} keys %{$counts{St +atus}}; print "Total failures: $totalfails\n";

Looking at the long line which does all the work, from "grep" to the end extracts the hash keys which match your criteria (>1). The "for" then iterates over these keys, assigning them to $_ each time. The "+=" extracts the value associated with each key from the original hash and cumulatively adds them to $totalfails. HTH.

  • Comment on Re^7: Turning regex capture group variables into arrays, then counting the number of objects in the array
  • Select or Download Code

Replies are listed 'Best First'.
Re^8: Turning regex capture group variables into arrays, then counting the number of objects in the array
by Djay (Novice) on Dec 06, 2018 at 13:06 UTC
    I believe its now working! Full script below, cobbled together thanks to the wonderful work of the Perl Monks!
    #!/usr/bin/perl use strict; use Data::Dumper ; my $str = `bpdbjobs`; my $filename = 'bpdbjobs.txt'; open(FH, '>', $filename) or die $!; print FH $str; close(FH); open(FH, '<', $filename) or die $!; my $fmt = 'A5 A15 A1 A7 A6'; my %counts = (); my @col = ('JobID','Col2','Type','State', 'Status','Policy','Schedule','Client', 'Dest Media Svr','Active PID'); # 10 cols my $totalfails = 0; while (<FH>){ next unless /\S/; # skip blank lines next if /^\s+JobID/; # skip header chomp; my @f = unpack $fmt,$_; s/^\s+|\s+$//g for @f; # trim spaces # count each column for my $n (0..$#col){ ++$counts{$col[$n]}{$f[$n]}; } print join "\|",@f,"\n"; # check } $totalfails += $counts{'Status'}{$_} for grep {$_ > 1} keys %{$counts{ +'Status'}}; print Dumper \%counts; printf "Statistic.SUCCESSFULL = %d\n",$counts{'Status'}{'0'}; printf "Statistic.PARTIAL = %d\n",$counts{'Status'}{'1'}; printf "Statistic.FAILS = $totalfails\n"; close(FH);