in reply to Re: date & file hash
in thread date & file hash

gotcha.

The dates are in format of mm/dd/yyyy & the filenames are ####.log. For example:

11/01/2001 log1.log 11/02/2001 log2.log ... 09/05/2003 log600.log

Replies are listed 'Best First'.
Re: Re: Re: date & file hash
by shenme (Priest) on Sep 05, 2003 at 19:40 UTC
    People like dates in the form mm/dd/yyyy, but they sure don't sort nicely.   Please use the below as a source of ideas.
    #!/usr/bin/perl -w use strict; use warnings; my %the_hash = ( 'log1.log' => '11/01/2001', 'log2.log' => '11/02/2001', 'floppy-earred.dog' => '01/02/1999', 'log600.log' => '09/05/2003', ); my( @sortedkeys, %sortedlbls ); use CGI; $| = 1; my $q = CGI->new(); print $q->header, $q->start_html('Test for waxmop'), $q->h1('Test checkbox_group'), $q->start_form; # Sort by the keys (filenames), display "filename date" @sortedkeys = sort keys %the_hash; %sortedlbls = map { $_ , "$_ $the_hash{$_}" } @sortedkeys; print $q->checkbox_group( -name => 'byfilename', -values => [ @sortedkeys ], -labels => { %sortedlbls }, -linebreak => 1 ); print "<hr>\n"; # Sort by the values (date strings), display "date filename" @sortedkeys = sort { $the_hash{$a} cmp $the_hash{$b} } keys %the_h +ash; %sortedlbls = map { $_ , "$the_hash{$_} $_" } @sortedkeys; print $q->checkbox_group( -name => 'bydatestring', -values => [ @sortedkeys ], -labels => { %sortedlbls }, -linebreak => 1 ); print "<hr>\n"; # Sort by the 'real' date value using Schwartzian transform @sortedkeys = map { $_->[0] } # get back our saved k +ey sort { $a->[3] <=> $b->[3] # sort by year || $a->[1] <=> $b->[1] # then by month || $a->[2] <=> $b->[2] # then by day of month } map { # make a temporary list of arrays, wit +h each [ $_, # array's first element the original k +ey # and the next three elements the date $the_hash{$_} =~ m!^(\d\d)/(\d\d)/(\d\d\d\d +)$! # string pieces mm dd yyyy ] } keys %the_hash; %sortedlbls = map { $_ , "$the_hash{$_} $_" } @sortedkeys; print $q->checkbox_group( -name => 'bydatevalue', -values => [ @sortedkeys ], -labels => { %sortedlbls }, -linebreak => 1 ); print "<hr>\n"; print $q->endform; print $q->end_html;
    Output:

    Test checkbox_group

    □ floppy-earred.dog 01/02/1999
    □ log1.log 11/01/2001
    □ log2.log 11/02/2001
    □ log600.log 09/05/2003

    □ 01/02/1999 floppy-earred.dog
    □ 09/05/2003 log600.log
    □ 11/01/2001 log1.log
    □ 11/02/2001 log2.log

    □ 01/02/1999 floppy-earred.dog
    □ 11/01/2001 log1.log
    □ 11/02/2001 log2.log
    □ 09/05/2003 log600.log

      ok. you are the bestest monk ever ... do you guys have MVM (most valuable monk)? You're getting my vote shenme.

      gratci

        Nah, just avoiding work and practicing for my next job....     Wish somebody would answer the second part of your question.   It would probably be interesting.
        Yeeks! Da boss! (bye)