From 58de849fa2ac5fff547b9294b3c76916e920bacb Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sat, 26 Aug 2023 11:22:19 -0400 Subject: [PATCH] using nsort_by --- script.pl | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/script.pl b/script.pl index 4b4a71a..92ca833 100644 --- a/script.pl +++ b/script.pl @@ -3,9 +3,10 @@ use v5.32; use HTML::TableExtract; use File::Slurp; use Data::Dumper; -use List::Util qw(sum); use Text::SimpleTable::AutoWidth; use WWW::Mechanize (); +use List::AllUtils qw/ sum pairmap /; +use List::UtilsBy qw/ nsort_by /; use experimental qw/ signatures /; @@ -70,21 +71,21 @@ sub generate_output_table ($ts) { } # create hash with each comptuer and its average GHz Days per day - my %ranks; - foreach my $key ( keys %$list ) { - my $mean = sum( @{ $list->{$key} } ) / @{ $list->{$key} }; + my %ranks = pairmap { + my $mean = sum( @$b ) / @$b; my $rounded = int( $mean + 0.5 ); - $ranks{$key} = $rounded; - } + $a => $rounded; + } %$list; # sort hash by the average and print to screen my $tbl = Text::SimpleTable::AutoWidth->new( captions => [qw/ Computer GHZDaysPerDay /] ); - foreach - my $key ( reverse sort { $ranks{$a} <=> $ranks{$b} } keys(%ranks) ) { - $tbl->row( $key, $ranks{$key} ); - } + $tbl->row( @$_ ) for + reverse + nsort_by { $_->[1] } + pairmap { [$a,$b] } + %ranks; return $tbl; }