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

Dear Monks,

I've this problem with sort:
($result) = sort ($val1, $val2) ; print $result ;
If $val1 or $val2 is undef $result is undef too. Is there a way to change this behaviour or maybe a smart solution ?

Thanks
Luca

Replies are listed 'Best First'.
Re: sort and undef
by Hena (Friar) on Dec 12, 2005 at 13:42 UTC
    Do you mean like this:
    ($result) = sort grep{defined()} ($val1, $val2) ;
    Note. Usually it might be a good idea to tell whats the wanted result.
Re: sort and undef
by Fletch (Bishop) on Dec 12, 2005 at 13:38 UTC

    Perhaps sort grep defined, ($val1, $val2)?

Re: sort and undef
by gjb (Vicar) on Dec 12, 2005 at 13:44 UTC

    You can always supply your own sort criterium in the form of a piece of code with the variables $a and $b that such that undef is larger than any other value. For more information, see the sort() docs.

    Hope this helps, -gjb-

Re: sort and undef
by jeanluca (Deacon) on Dec 12, 2005 at 14:10 UTC
    Thnx, all this solved my problems with sort!!
    Luca