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

I have spent an afternooon trying to work this one out and it has me stumped, if anyone can surggest a resource which can do this I will be enturnally grateful.

My problem is I have a data structure as follows

@job_data = ({'name'='job1', 'runs'='19:20'}, {'name'='job2', 'runs'='17:22'}, {'name'='job3', 'runs'='19:25'})
The hashes come from a number of XML files and there are about 70 hahses in the array.
What I need to do is sort the hashes in to time order on the string representation of time in the 'runs' value.
Can anyone offer help. Thanks in advance.

Update
With the advice from dws I used this sub to deal with my problem, I'm sure someone will come up with a simple one liner that does the same, but I'm just a hopeless sinner.

sub by_time{ my $a_time = $a->{'starts'}; my $b_time = $b->{'starts'}; my @aparts = split ':', $a_time; my @bparts = split ':', $b_time; #print $aparts[0]."\t".$bparts[0]."\n"; if ($aparts[0] > $bparts[0]){return 1} if ($aparts[0] < $bparts[0]){return 0} else { if ($aparts[1] > $bparts[1]){return 1} if ($aparts[1] < $bparts[1]){return 0} else{return 1} }; }

Replies are listed 'Best First'.
Re: Sorting an array of hashes
by dave_the_m (Monsignor) on Jul 28, 2004 at 08:42 UTC
    @job_data2 = sort { $a->{runs} cmp $b->{runs} } @job_data;

    Dave.

      Thanks I was miles out on this one. I was thinking it was going to be difficult so I made it difficult I should have remembered that "Perl makes difficult things easy"
Re: Sorting an array of hashes
by dws (Chancellor) on Jul 28, 2004 at 08:47 UTC

    I assume that within the anonymous hash you meant to write "=>" instead of "=".

    You're looking at doing something like:
    my @sorted = sort { timecompare($a->{runs}, $b->{runs}) } @job_data;
    where timecompare is a routine you'll need to supply (i.e., "left as an exercise") that takes two strings of the form '19:20', does whatever manipulations is necessary to compare them, and returns -1, 0, or 1. Since you don't show whether a shorter time is shown as "03:01" vs. "3:01", a straight cmp may or may not work for you.

      Your dead right here and in essence this is what I've been trying, but had completely the wrong idea about how the values got to the sub routine. The other fist 2 answers are fine but only deal with string order. Thanks for your help!!
Re: Sorting an array of hashes
by davorg (Chancellor) on Jul 28, 2004 at 08:47 UTC

    Would be nice if you showed us the code you've tried so we can explain where your misunderstandings are.

    But this will do what you want.

    @job_data = sort { $a->{runs} cmp $b->{runs} } @job_data;
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Good piont but if you saw the complicated way I went about it you would understand my embrassment.
      I guess K.I.S.S should be kept well and truely in my field of vision.
Re: Sorting an array of hashes
by Anonymous Monk on Jul 28, 2004 at 20:26 UTC
    You should look into XML::Simple for turning your keys around. Much more convenient, much less work.