Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Sorting by date

by Roger (Parson)
on Jan 06, 2004 at 01:02 UTC ( #318989=note: print w/replies, xml ) Need Help??

in reply to Sorting by date

use strict; use Data::Dumper; my @date = qw/ 1.13.04 1.12.04 3.13.04 /; my @sorted_date = map { $_->[0] } # retrieve the original date sort { $b->[1] <=> $a->[1] } # sort the normalized date map { my @n = /(\d+)\.(\d+)\.(\d+)/; # normalize the date my $d = sprintf "%02d%02d%02d", @n[2,0,1]; [$_, $d] } @date; print Dumper(\@sorted_date);
and the output:
$VAR1 = [ '3.13.04', '1.13.04', '1.12.04' ];

Replies are listed 'Best First'.
Re: Re: Sorting by date
by Anonymous Monk on Jan 06, 2004 at 01:12 UTC
    The only problem with this is, I'm not working with arrays. I have my list setup like: name => date:message:email.

    So to begin with, I need to

    foreach (keys %list) { my $value = $list{$_}; my ($date, $message, $email) = split(/:/, $value); print "The date is: $date"; # 1.11.04 or 8.3.04 }
    Since I'm not working with arrays, I don't think I can map and compare unless I'm confused as to what I'm doing. But IF I can get it to print by date, how do I get it to print a new table for each month?

    Thanks for your help.

      use strict; use Data::Dumper; my %list = ( A => '1.13.04:Record:A', B => '1.12.04:Record:B', C => '3.13.04:Record:C', ); my @sorted_date = map { $_->[0] } sort { $b->[1] <=> $a->[1] } map { my @n = /^(\d+)\.(\d+)\.(\d+):/; my $d = sprintf "%02d%02d%02d", @n[2,0,1]; [join('.',@n), $d] } values %list; print Dumper(\@sorted_date);

      OK, here's another variant of the sort that produces the HTML table as required...
      use strict; use Data::Dumper; my %list = ( A => '1.13.04:Record:A', B => '1.12.04:Record:B', C => '3.13.04:Record:C', D => '3.13.02:Record:D', ); my @sorted_date = map { [ $_->[1], $_->[0], $_->[2] ] } sort { $b->[1] <=> $a->[1] } map { my @n = $list{$_} =~ /^(\d+)\.(\d+)\.(\d+):/; my $d = sprintf "%02d%02d%02d", @n[2,0,1]; [join('.',@n), $d, $_] } keys %list; my $month; foreach (@sorted_date) { my $m = substr($_->[0], 0, 4); # retrieve YYMM if ($month ne $m) { print "</table>\n" if $month; $month = $m; print "<table>\n"; } print "<tr><td>$_->[1]</td><td>HASH key: $_->[2]</td></tr>\n"; } print "</table>\n" if $#sorted_date >= 0;

      And the output is:
      <table> <tr><td>3.13.04</td><td>HASH key: C</td></tr> </table> <table> <tr><td>1.13.04</td><td>HASH key: A</td></tr> <tr><td>1.12.04</td><td>HASH key: B</td></tr> </table> <table> <tr><td>3.13.02</td><td>HASH key: D</td></tr> </table>
        I thank you very much for your codes. I used the first part and it works AWESOME. Everything sorts so cleanly and perfectly. I can't get the table printing to work though, I get: Can't use string ("1.18.05") as an ARRAY ref while "strict refs" in use at ... So for some reason, something is acting up. But the first part of your code works wonders and I thank you very much for that!
        This is very interesting, though I can certainly admit I've always been afraid of looking into MAP and SORT (just the way it looks so complicated makes me cringe). One thing after looking over this a number of times that I still don't get is... You are taking an array and putting only the date inside, right? This seems to be what the original poster wanted anyway, but with mapping how are you supposed to draw back the rest of the %list's values?
        A => '1.13.04:Record:A', B => '1.12.04:Record:B', C => '3.13.04:Record:C', D => '3.13.02:Record:D',
        You are stripping apart the date into $_->[1], how would you find the Record or the letter afterwards? Let's say $_->[1] is 1.12.04 this time. How would you find Record and B and take that with it so you can use it later? Or is that data lost and only the date can be retrieved after you've mapped?

        At this level of confusion, unless this question makes sense and can be answered without digging the hole any deeper, even attempting a project which requires mapping or sorting like this is just not likely to happen in the near future.

        Thanks for your time, everyone.

        "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

        I am sorry to bug you again. I've fiddled with this code a great deal but then I realized I had to change the script just a little bit. Now I'm lost as to how to make the little change I need to get it working again.

        The date we separated earlier was month, day, year. I now have to make it seperate by day, month, year (Britian's way of doing dates). I tried changing the line @n[2,0,1]; to @n[1,0,2]; but no changes occured. Is there something else I have to change to make it separate the string into day, month, year instead of month, day, year?

        Again, sorry for bothering you. Hopefully there is a quick fix for this.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://318989]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2018-05-26 12:38 GMT
Find Nodes?
    Voting Booth?