Sorting and undefined values

Robin Barker Robin.Barker at
Tue Dec 12 17:23:08 GMT 2006

My first thought is that there is a typo:
  $foo->{$a}->{'x'} cmp $foo->{$b}->{'x'} ||
  $foo->{$b}->{'y'} cmp $foo->{$b}->{'y'}

My second thought is to worry about precendence:
  $foo->{$a}->{'x'} cmp $foo->{$b}->{'x'} or
  $foo->{$a}->{'y'} cmp $foo->{$b}->{'y'}
  ($foo->{$a}->{'x'} cmp $foo->{$b}->{'x'}) ||
  ($foo->{$a}->{'y'} cmp $foo->{$b}->{'y'})


-----Original Message-----
From: at
[ at]On Behalf Of Earle Martin
Sent: 12 December 2006 17:00
To: London Perl Mongers
Subject: Sorting and undefined values


I have this code:

my @sorted = sort {
  $foo->{$a}->{'x'} cmp $foo->{$b}->{'x'} ||
  $foo->{$b}->{'y'} cmp $foo->{$b}->{'y'}
} keys %$foo;

This only produces the keys of items where 'x' is defined. What do I
need to do to sort first on 'x' if it's defined, and then on 'y', or
just 'y' if 'x' is undefined?

Thanks in advance,


Earle Martin

This e-mail and any attachments may contain confidential and/or
privileged material; it is for the intended addressee(s) only.
If you are not a named addressee, you must not use, retain or
disclose such information.

NPL Management Ltd cannot guarantee that the e-mail or any
attachments are free from viruses.

NPL Management Ltd. Registered in England and Wales. No: 2937881
Registered Office: Serco House, 16 Bartley Wood Business Park,
                   Hook, Hampshire, United Kingdom  RG27 9UY

More information about the mailing list