Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

looking for a module to handle 'highscore' lists

by domm (Chaplain)
on Mar 20, 2005 at 15:57 UTC ( #441021=perlquestion: print w/replies, xml ) Need Help??

domm has asked for the wisdom of the Perl Monks concerning the following question:

I'm currently planning to add a new feature to the CPANTS site: I want the list of 'top' authors to list the previous ranking of an author (like in music charts). Additionally, I want authors with the same average kwalitee listed on the same 'spot' ('ex aequo', as in results of eg downhill skiing)

Are there modules on CPAN that calculate somthing like this?

Given an input like:

[ [RUBYKAT,15.97], [SAPER,15.78], [PETDANCE,15.5], [AMBS,15.5], [BARBIE,15.45], ... ]
I would like output like this:
(where ^ and v should be up/down arrows representing a change to the previous status.)

If there isn't a module (or several doing parts of this), I'll probably roll my own. Any naming suggestions?

-- #!/usr/bin/perl for(ref bless{},just'another'perl'hacker){s-:+-$"-g&&print$_.$/}

Replies are listed 'Best First'.
Re: looking for a module to handle 'highscore' lists
by jbrugger (Parson) on Mar 20, 2005 at 16:25 UTC
    I think Statistics::Lite could help you, it's pure perl and easy to use.
    Else, a simple sort algorithm should help you, if you have your data stored in a hash like
    # untested... %list = ( RUBYKAT=>15.97 , SAPER=>15.78 , PETDANCE=>15.5 , AMBS=>15.5 , BARBIE=>15.45 , ); sub sortDesc { $list{$b} <=> $list{$a}; } foreach $key (sort sortDesc (keys(%list))) { print "$list{$key} \t $key \n"; }

    but i don't think there is a specific cpan module available to this.
    anyway, i would not use an array of arrays for this.
    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
Re: looking for a module to handle 'highscore' lists
by TedPride (Priest) on Mar 20, 2005 at 17:01 UTC
    EDIT: Oops, had to update code for people with identical scores, and people who weren't on previous listing.
    use strict; use warnings; my (%lrank, $c, $s, $name, $score, $lrank, $lscore); my @lrank = ('RUBYKAT','SAPER','PETDANCE','AMBS','BARBIE'); my @nrank = ( ['PETDANCE',12.4], ['SAPER',15.78], ['AMBS',15.5], ['RUBYKAT',15.97], ['BARBIE',16.06], ['NEWBIE',12.4], ); $lrank{$_} = $c++ for @lrank; $s = '%' . (length($#nrank+1)+1) . 's'; @nrank = sort {@$b[1] <=> @$a[1] || @$a[0] cmp @$b[0]} @nrank; $c = 0; $lscore = 0; for (@nrank) { ($name, $score) = @$_; if (exists $lrank{$name}) { $lrank = $lrank{$name}; print $lrank > $c ? '^' : $lrank < $c ? 'v' : ' '; } else { print ' '; } print sprintf $s, $lscore == $score ? '' : $c; print ": $name\n"; $lscore = $score; $c++; }
    Note that people of the same score are currently sorted alphabetically. Could be you'd want them sorted according to first come, first served, in which case a more complicated algorithm would be necessary.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://441021]
Approved by davidj
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2021-08-06 00:10 GMT
Find Nodes?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)

    Results (44 votes). Check out past polls.