DarrylLundy
Technical User
I am newish to Perl, and am struggling with sorting an array of hashes on multiple fields. I have used the code given in Programming Perl, 3rd ed., under the sort function.
So my code:
along with the following test data:
gives me the following warning message:
- referencing the code '$b->{ORDINAL} cmp $a->{ORDINAL}'
and the data is not sorted in the right order. I get:
1st Baronet Abyn
3rd Duke Bedford
1st Duke Bedford
2nd Duke Bedford
rather than the intended:
1st Baronet Abyn
1st Duke Bedford
2nd Duke Bedford
3rd Duke Bedford
I don't see what values are uninitialized, and therefore why the sort fails to do anything. What am I doing wrong?
Regards
Darryl
So my code:
Code:
my @s;
my @t;
sub bytitle {
$b->{TITLE} cmp $a->{TITLE}
||
$b->{SURNAME} cmp $a->{SURNAME}
||
$b->{ORDINAL} cmp $a->{ORDINAL}}
@s = sort bytitle @t;
along with the following test data:
Code:
$t[0]{TITLE} = "Baronet";
$t[0]{SURNAME} = "Abyn";
$t[0]{ORDINAL} = " 1st";
$t[1]{TITLE} = "Duke";
$t[1]{SURNAME} = "Abercorn";
$t[1]{ORDINAL} = " 1st";
$t[2]{TITLE} = "Duke";
$t[2]{SURNAME} = "Abercorn";
$t[2]{ORDINAL} = " 3rd";
$t[3]{TITLE} = "Duke";
$t[3]{SURNAME} = "Abercorn";
$t[3]{ORDINAL} = " 2nd";
gives me the following warning message:
Code:
use of uninitialized value in string comparison (cmp) at xx.pl line x
and the data is not sorted in the right order. I get:
1st Baronet Abyn
3rd Duke Bedford
1st Duke Bedford
2nd Duke Bedford
rather than the intended:
1st Baronet Abyn
1st Duke Bedford
2nd Duke Bedford
3rd Duke Bedford
I don't see what values are uninitialized, and therefore why the sort fails to do anything. What am I doing wrong?
Regards
Darryl