PDA

View Full Version : order / cmp


ynotlim
09-21-2007, 05:59 PM
I want to order this array by the first column.


%tour = (
'0', 'No tour',
'1', '12:30 p.m. - Housing',
'2', '12:30 p.m. - Campus ,
'3', '1:30 p.m. - Facilities',
'4', '1:30 p.m. - Staff',
);


the following only does the second column


sub byname { $tour {$a} cmp $tour {$b} }
foreach $code (sort byname6 keys %tour ) {
print "$tour {$code}\n";
}


Thanks in advance

FishMonger
09-21-2007, 06:29 PM
I want to order this array by the first column.


%tour = (
'0', 'No tour',
'1', '12:30 p.m. - Housing',
'2', '12:30 p.m. - Campus ,
'3', '1:30 p.m. - Facilities',
'4', '1:30 p.m. - Staff',
);


Solutionfor my $key (sort keys %tour) {
print "$key => $tour{$key}\n";
}


the following only does the second column


sub byname { $tour {$a} cmp $tour {$b} }
foreach $code (sort byname6 keys %tour ) {
print "$tour {$code}\n";
}



If you run your script with the warnings and strict pragmas that code will produce a compilation error and once you fix that, it will produce a number of warnings i.e., "Use of uninitialized value in concatenation (.) or string at ..."

FishMonger
09-21-2007, 06:37 PM
%tour = (
'0', 'No tour',
'1', '12:30 p.m. - Housing',
'2', '12:30 p.m. - Campus ,
'3', '1:30 p.m. - Facilities',
'4', '1:30 p.m. - Staff',
);



Is better written as:
my %tour = (
0 => 'No tour',
1 => '12:30 p.m. - Housing',
2 => '12:30 p.m. - Campus',
3 => '1:30 p.m. - Facilities',
4 => '1:30 p.m. - Staff',
);