Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Ohhh Great Monks,

I have a hash consisting of filenames & there creation time that I am posting to html as follows:

$q->checkbox_group(-name=>'dates', -values=>[ keys %labels ], -labels=> \%labels
Two issues arise upon implementation:

1) The dates are not sorted. Is there any way to sort the  keys %labels as well as the \%labels?

2) It would be much nicer appllication wise if all the dates that are on a monday are under a monday column, tuesdays under a tuesday column, etc. The problem is that they all need to be in the same checkbox_group. Can this be done?

thank you for your time monks!!

Replies are listed 'Best First'.
Re: date & file hash
by dragonchild (Archbishop) on Sep 05, 2003 at 16:53 UTC
    1. Yes.
      # You might have to expand the sort. Exercise for the reader and all t +hat. :-) my @keys = sort { $labels{$a} cmp $labels{$b} } keys %labels; $q->checkbox_group( -name => 'dates', -values => \@keys, -labels => \%labels, );
      You don't need to sort the labels, just the keys.
    2. Yes. I don't have time right now to explain, but I'm sure someone else will.

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: date & file hash
by hardburn (Abbot) on Sep 05, 2003 at 16:52 UTC

    For #1--because Perl makes everything extermely difficult, you have to say:

    -values=>[ sort keys %labels ],

    Whew!

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: date & file hash
by shenme (Priest) on Sep 05, 2003 at 17:04 UTC
    In general to get hash values in same order as the sorted keys you need something like (untested):
    my @sortedkeys = sort keys %the_hash; my @sortedvals = @the_hash{@sortedkeys};
    I'm afraid you'll have to give an example of which values, filenames or creation times, are used as keys and which as values.   And which did you want displayed and which as the returned checkbox values?   It would seem you would want the displayed strings (-labels) to show both bits of info and the returned values (-values) would be the filenames? Show us more of your code?
      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

        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