in reply to Re^2: Condensed rank table output
in thread Condensed rank table output

> guidance is OK :)

and here some code to correct the other contributions ;-)

I also added some printf to prettify the output. (though it might be easier to output the score before the names)

use strict; use warnings; #use Data::Dump qw/pp dd/; # https://perlmonks.org/?node_id=11105802 # data stolen from tybaldt my %score_by_name = ( Alec => 14, Alice => 12, Bob => 8, Donny => 13, Jacob => 13, Mike => 11, Sarah => 13, First => 17 ); # invert %score_by_name to HoA my %names_by_score; while ( my ($name,$score) = each %score_by_name) { push @{$names_by_score{$score}}, $name; } #pp \%names_by_score; my $rank=0; for my $score ( sort {$b <=> $a} keys %names_by_score ) { my @names = @{$names_by_score{$score}}; # calculate ranks my $rank_str = $rank+1; $rank += @names; $rank_str .= "-$rank" if @names >1; # span if necessary # output printf "%3s. %-20s - %2d\n", $rank_str, join (", ",@names), $score; # printf "%3s. (%2d) %s\n", # $rank_str, $score, join (", ",@names); }

1. First - 17 2. Alec - 14 3-5. Donny, Jacob, Sarah - 13 6. Alice - 12 7. Mike - 11 8. Bob - 8

alternative output

1. (17) First 2. (14) Alec 3-5. (13) Sarah, Donny, Jacob 6. (12) Alice 7. (11) Mike 8. ( 8) Bob

updates

  • inverted sort makes reverse obsolete

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

  • Replies are listed 'Best First'.
    Re^4: Condensed rank table output
    by tlk (Acolyte) on Sep 08, 2019 at 07:32 UTC

      Thanks for the pretty-printing implementation.

      For now tho I'm implementing the 'core functionality' (and mimicking an already existing layout). Once I get to spicing it up this will surely come in handy!