Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

sorting routines, mapping and code style

by moxliukas (Curate)
on Jul 19, 2002 at 13:55 UTC ( [id://183233]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks,

I am not very proficient at Perl (yet), therefore I would like to as you for some help.

I have failed to start Robomonk because it needed some modules that refused to compile for me (probably because I didn't have root access), therefore I decided to wrtie a simple IRC bot, that would fetch my XP and XP of my Lithuanian fellows. After some coding the bot runs OK, however I am not very happy with it, mostly because of the weird sorting routine that I wrote (I am sure it could be simplyfied) and some other bits and pieces. The reason why this bot doesn't fetch the XP values straight from perlmonks, but from lietuvoje.lt is because I have written this code before for displaying XP stats on a mobile phone via WAP/WML (reusing code is a Good Thing (TM), right?) However I feel that this code should also be rewritten to fetch things straight from perlmonks, because it is sloooow.

So what I am asking you is to give me ideas how to make this code more efficient (especially that ugy sorting routine)

The input at lietuvoje.lt looks like this:

moxliukas: 188 mr2: 202 m3LLow: 21 Nightblade: 18 TuXaS: 9

I want to sort that in descending order by XP and display it on IRC. Here is the code:

#!/usr/bin/perl -w use strict; use Net::IRC; use LWP::Simple; my $kanalas = '#perl'; my $irc = new Net::IRC; my $conn = $irc->newconn(Nick => 'Robomonk', Server => 'irc.delfi.lt', Port => 6667, Ircname => 'blah...'); sub on_connect { my $self = shift; # print "Joining "; $self->join($kanalas); # $self->privmsg('#robomonk', 'Hi there'); } sub on_public { my ($self, $event) = @_; my @to = $event->to; my ($nick, $mynick) = ($event->nick, $self->nick); my ($arg) = ($event->args); # my ($a, $b); if ($arg =~ /^!XP/) { my $cont = get('http://www.lietuvoje.lt/wap/perl.php'); my @heh = split(/\n/, $cont); my %hmm = map { /([^:]+): (\d+)/; $1 => $2 } @heh; @heh = map {$_ . ': ' . $hmm{$_}} sort { $hmm{$b} <=> $hmm{$a} + } keys %hmm; for my $a (@heh) { $self->privmsg($kanalas, $a); } # print $cont; } if ($arg =~ /^!quit/ && ($nick eq 'moxliukas' || $nick eq 'NB' || +$nick eq 'mr2')) { $self->quit('Einu papostint i perlmonks...'); exit(0); } } $conn->add_handler('public', \&on_public); $conn->add_global_handler('376', \&on_connect); $irc->start;

PS. As I am writting this, I have already noticed that I should really give more descriptive names for variables... I promise I will always try to do this in future ;)

Replies are listed 'Best First'.
Re: sorting routines, mapping and code style
by Abigail-II (Bishop) on Jul 19, 2002 at 15:01 UTC
    Well, you can speed up your sort by using a GRT. Something like the following should do the trick:
    @heh = map {s/^(\d+):(.*)/$2: $1/; $_} sort map {sprintf "%10d:$_"} keys %hmm;
    But considering the amount of data in %hmm, that's not going to be your bottleneck, so why bother?

    Abigail

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-23 17:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found