Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Sorting Issues :(

by Masem (Monsignor)
on Jul 18, 2001 at 21:28 UTC ( #97782=note: print w/replies, xml ) Need Help??

in reply to Sorting Issues :(

In this situation, a Schwatzian Transformation is going to be necessary, as you'll need to have some way to convert from "HH:MM AP" to, say, a 24hr time. Assuming that the times are all within the same day, you can use hand coded stuff, but there are always modules like Date::Manip that will convert the time string to a epoch time.

But a simple starting point could be as follows:

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 - || "You've left the lens cap of your mind on again, Pinky" - The Brain

Replies are listed 'Best First'.
Re: Re: Sorting Issues :(
by LostS (Friar) on Jul 18, 2001 at 21:52 UTC
    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?
      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.

      Throw down the gun and tiara and come out of the float!

      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 - || "You've left the lens cap of your mind on again, Pinky" - The Brain

        Man one day I hope to be as well versed in perl as you. :)

      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

        All these suggestions rock... I work for a governent agency so... anyway to idiot proof the stuff helps :)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://97782]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2023-01-28 09:46 GMT
Find Nodes?
    Voting Booth?

    No recent polls found