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

Re: Sorting Dates

by gjb (Vicar)
on Nov 24, 2005 at 10:40 UTC ( #511392=note: print w/ replies, xml ) Need Help??


in reply to Sorting Dates

If you want to go with your original approach, the following maybe useful:

my @dates = qw(5/7/2001 3/10/2005 1/1/1996 2/1/1993); foreach my $date (@dates) { if ($date =~ m|(\d{1,2})/(\d{1,2})/(\d{4})|) { my $new_date = sprintf("%d/%02d/%02d", $3, $2, $1); ... } }

Hope this helps, -gjb-


Comment on Re: Sorting Dates
Download Code
Re^2: Sorting Dates
by josephjohn (Acolyte) on Nov 24, 2005 at 10:51 UTC
    Thanks for the suggestion gjb. Does that sort the hash too. I need to sort the dates, find the last date, and perform some calculations later in the script. Also please let me know what qw() is used for.

      This doesn't sort the hash, it only converts the date from D/M/YYYY to YYYY/MM/DD so that it can be used as a key in your %datecount hash. This hash can be sorted the way you do it in your code fragment.

      As to qw:

      $ perldoc -f qw qw/STRING/ Generalized quotes. See "Regexp Quote-Like Operators" in perlop.
      It returns a list of words without doing interpolation on them. It is just a convenient way of writing:
      my @dates = ('5/7/2001' '3/10/2005' '1/1/1996' '2/1/1993');

      Hope this helps, -gjb-

      >Does that sort the hash too

      You cannot sort a hash : a hash is by construction with no particular order. What you can do is sort the keys of a hash and print the corresponding values.

      When you do  foreach (keys %hash) you get the keys in an order that is undefined although always the same. But it will change if you add or remove keys, therefore if you need a specific order you should do  foreach ( sort { my sort function} keys %hash )

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2014-08-01 03:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (256 votes), past polls