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