Do you know where your variables are? | |
PerlMonks |
Re: Sort questions (use a GRT)by grinder (Bishop) |
on Jan 28, 2005 at 22:03 UTC ( [id://426137]=note: print w/replies, xml ) | Need Help?? |
This is not too shabby to start with, you're on the right track. To sort by the day of the week, use a hash as a lookup, to retrieve the day value:
You might want to make Sunday => 7 if you want it to sort higher, to put the weekend at the end. I'm using a qw() quote-word construct, which returns a list and assigns that to a hash. Just a bit less typing. For the timestamps, if you can guarantee that they will always follow a ddd:dd:dd format, you can just sort them alphabetically as you are already doing To combine them all into an efficient sort statement, use a Guttman-Rosler Transform (GRT)*. The idea is to build a prefix to the string so that you can use a bare sort operation, and then throw away the prefix after the sort and recover the original record.
Since the first field of the record is also part of the sort criteria, you don't actually have to make it part of the prefix. So you only have to add on a munged day of week and the timestamp to have it sort correctly. Then, after the sort, split it up on % and throw the first two away to recover your record. If the array can be really large, say, over 200_000 entries, then you should consider preparing data files so that the operating system sort command can be used. Otherwise I'd just stick with Perl. * see Advanced Sorting - GRT - Guttman Rosler Transform for a discussion on the GRT. - another intruder with the mooring in the heart of the Perl
In Section
Seekers of Perl Wisdom
|
|