In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] (Rob Richardson) writes: >Greetings! > >In the train schedule program that you are all probably heartily sick >of by now, I have added a ScheduleDay class that represents all trains >running on a given day. It has a method that returns an array of >references to all of the Train objects for that day. I want to sort >those trains on the times people have to show up for them. The Train >class has a GetCrewCall() method that returns a string containing the >crew call time in HH:MM form, with two digits guaranteed for the hours. > Thus, "cmp" will suffice for the comparison. But I'm not sure how to >set up the sort. Here's what I have: > >sub GetTrainsByTime >{ > my $self = shift; > my @result; > my @temp; > my $aTrain; > my $aTrainName; > > # First we build a temporary array of references to trains > foreach $aTrainName (keys $self) ^ % missing > { > push @temp, $self{aTrainName}; ^ $ missing
> } > > @result = sort {$a->GetCrewCall() cmp $b->GetCrewCall()} @temp; > return @result; >} (1) If you wanted to put the keys of a hash into an array, just do it all at once: @temp = keys %$self (2) If you want to sort the keys of a hash, there's no need to put them into an array. sort takes a list as input: return sort { $a->GetCrewCall cmp $b->GetCrewCall } keys %self >I'm pretty sure this will work, since $a and $b will be references to >Train objects, and whatever is inside the brackets is just a plain >ordinary block of code. Is this correct? Yes. >In general, $a and $b are going to be elements of the array being >sorted, aren't they? Yes. -- Peter Scott http://www.perldebugged.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]