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 ;)


In reply to sorting routines, mapping and code style by moxliukas

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.