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

Hello Monks, i got a newbie question here.

I can't fully understand how is sort() function works with preprocessing argument ( sort {precode} @array; ) and need your help:

@userz_tmp = qw/goodman hsytva %pisyun [error] Gerich Fair CYCLER ~shu +rmann &BECHED sp w %sanekdark FIXER lizergin @XUEC/; foreach(sort sorting @userz_tmp) { print "$_\n"; } sub sorting { lc($a) cmp lc $b; }
As you can see it's unreal ircd's permissions, so what i need to put in subroutine 'sorting' to get output like:

~nick     # 1: users with ~
~nick2
&nick     # 2: users with &
@nick     # 3: users with @
@nick2
%nick     # 4: users with %
+nick     # 5: users with +
nick8     # 6: users without ~&@%+ symbols in alphabetical order
nick9

Thank you.

Replies are listed 'Best First'.
Re: Sort names
by ikegami (Patriarch) on May 25, 2010 at 22:14 UTC
    my @order = ('~', '&', '@', '%', '+', ''); my %order = map { $order[$_] => $_ } 0..$#order; sub nick_cmp { my ($a_p, $a_n) = $a =~ /^([~&\@%+]?)(.*)/s; my ($b_p, $b_n) = $b =~ /^([~&\@%+]?)(.*)/s; $order{$a_p} <=> $order{$b_p} || lc($a_n) cmp lc($b_n) } my @users = qw( goodman hsytva %pisyun [error] Gerich Fair CYCLER ~shurmann &BECHED sp w %sanekdark FIXER lizergin @XUEC ); print "$_\n" for sort nick_cmp @users;
    ~shurmann &BECHED @XUEC %pisyun %sanekdark [error] CYCLER Fair FIXER Gerich goodman hsytva lizergin sp w
      I need to read about cmp. Thank you very much, ikegami.
        What about them isn't clear?