in reply to sort exceptions

Here's a slightly more generic version of Abigail's answer.

my %except = ( foot => -1, # sorts first hand => 1, # sorts last ); sub except_sort { return exists $except{$a} ? $except{$a} : exists $except{$b} ? -$except{$b} : $a cmp $b; } my @list = qw/leg hand eye foot jaw finger/; print join(" ", sort except_sort @list), $/;
--
3dan

Replies are listed 'Best First'.
Re: Re: sort hash exceptions
by japhy (Canon) on Jan 26, 2004 at 16:23 UTC
    The exists() aren't really necessary.
    sub except_sort { $except{$a} || -$except{$b} || ($a cmp $b) }

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      That gives "Use of uninitialized value in negation (-)". (Yes, I know, the example you responded to was missing use warnings;), so you need -($except{$b}||0) instead.