sub time_to_2400 {
my $time = shift;
my ( $hour, $min, $ap ) = ( $time =~ /^(\d{1,2}):(\d\d)\s([APM]{2})
+$/;
$hour = 0 if $hour == 12; #
return ( $hour * 100 + $minute + ( ( $ap eq 'AM' ) ? 0 : 1200 ) );
}
my @sorted_list =
map { $database_in[ $$_[0] ] }
sort { $$a[1] <=> $$b[1] }
map { [ $_, time_to_2400( (split /\t/, $database_in[ $_ ])[4] ) ]
+}
(0..@database_in-1);
Update and to the second part of the question, whenever new data is added to the database, given that you're using a flatfile, you can either run this sort again to get it into the right place, or better, read the flat file line by line and write to a new version of it, inserting the new item into the right place based on the time field, then write out the remaining part of the original flat file to the new file, and then move the new file over the old file when done.
-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com
||
"You've left the lens cap of your mind on again, Pinky" - The Brain
| [reply] [d/l] |
Sir,
Your suggections works great... Now I have another issue dealing with this. Due to my users are complete morons... They may set the time as a HH:MM{AM/PM} without that space... What should I do to check for that space and if it isn't there add it?
| [reply] |
Due to my users are complete morons...
Be careful with those stones: One might call someone who uses a sentence as poorly constructed as that a "moron", too.
Your users are not morons. They're human. What's more,
they're humans who don't really need to worry about whether
there's a space between before the AM/PM. Why should they
waste their human brain cycles dealing with something that the machine can handle just as well?
Remember why we're programmers: "Machines should work. People should think." The whole point of the machine is to take the drudgery off the human. Taking a snippy attitude at your users benefits no one.
Fortunately, Perl makes this sort of drudgery simple to handle, as you've already seen.
Take the path to the light of helpfulness, not to the darkness of insulting your customers.
xoxo,
Andy
--
Throw down the gun and tiara and come out of the float!
| [reply] |
Try this (in the regex...):
$time =~ /^(\d{1,2}):(\d\d)\s?([APM]{2})$/
#----------------------------^ Added this
# And actually, now that I come to think of it, the
# dealing with AM or PM problems may be better sorted using:
$time =~ /^(\d{1,2}):(\d\d)\s?(AM|PM)$/
Note that you ought to use this regex after the user submits the data and before you enter it, so that if a user gives something bogus, you catch it then, before the data goes in, as opposed to afterwards during the sort.
-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com
||
"You've left the lens cap of your mind on again, Pinky" - The Brain
| [reply] [d/l] |
To be on the safe side it's probably best to allow whitespaces (or none) in some more places which would turn the regex into:
$time =~ /^\s*(\d{1,2})\s*:\s*(\d\d)\s*([APM]{2})\s*$/The \s* matches zero or more whitespaces and as many as possible (greedy). So this allows for any of the following (_ equals whitespace):
_12:00_AM__
1_:_00_PM
2:20AM
And then you could allow for leaving out of AM/PM and assuming a 24 hour clock .... but /me gets carried away ;)
-- Hofmator
| [reply] [d/l] [select] |