Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Sorting an array

by powerhouse (Friar)
on Feb 15, 2003 at 00:14 UTC ( #235464=perlquestion: print w/ replies, xml ) Need Help??
powerhouse has asked for the wisdom of the Perl Monks concerning the following question:

I have a hash, with the months in it:

%month_array = ( 1 => "Jan", 2 => "Feb", 3 => "Mar", 4 => "Apr", 5 => "May", 6 => "June", 7 => "July", 8 => "Aug", 9 => "Sep", 10 => "Oct", 11 => "Nov", 12 => "Dec" );


I'm creating a popup_menu using CGI.pm. Here is the code for that:
popup_menu(-name=>"month_array", -values=>\%month_array, -labels=>\%month_array, -default=>$mon, -class=>"formfield")
That does not sort them from 1 to 12, (jan to dec).

I did have it like this:
popup_menu(-name=>"month_array", -values=>[sort keys %month_array], -labels=>\%month_array, -default=>$mon, -class=>"formfield")
That did not sort them either correctly.

How do I get them to sort from 1 to 12(values) or jan to dec(labels), using CGI.pm?

I used up both the ways I know to do it.

thx,
Richard

Comment on Sorting an array
Select or Download Code
Re: (nrd) Sorting an array
by newrisedesigns (Curate) on Feb 15, 2003 at 00:29 UTC
    use CGI; %month_array = ( '01' => "Jan", '02' => "Feb", '03' => "Mar", '04' => "Apr", '05' => "May", '06' => "June", '07' => "July", '08' => "Aug", '09' => "Sep", '10' => "Oct", '11' => "Nov", '12' => "Dec" ); print CGI::popup_menu(-name=>"month_array", -values=>[sort {$a cmp $b} keys %month_array], -labels=>\%month_array, -default=>$mon, -class=>"formfield")

    John J Reiser
    newrisedesigns.com

      Thank you. However, it is still putting it like this:

      value = 1 label = Jan
      ..10 ..Oct
      ..11 ..Nov
      ..12 ..Dec
      ..2 ..Feb
      ..3 ..Mar
      ...Etc.

      Here is what I put:

      popup_menu(-name=>"month_array", -values=>[sort {$a cmp $b} keys %month_array], -labels=>\%month_array, -default=>$mon, -class=>"formfield")

      Did I do it wrong?
      OOPS. I see, I did not add the 0 in front of the others :o)

      thx,
      Richard

        No problem. Glad to help!

        If you absolutely can't have the leading zero, do what the good monk pfaut did below.

        John J Reiser
        newrisedesigns.com

Re: Sorting an array
by pfaut (Priest) on Feb 15, 2003 at 00:45 UTC

    By default, sort performs a string sort. If you want it to do a numeric sort, you have to do something like this.

    popup_menu(-name=>"month_array", -values=>[sort { $a <=> $b } %month_array], -labels=>\%month_array, -default=>$mon, -class=>"formfield")

    Since the keys are the numbers 1-12, you could also try -values=>[1..12] and forget the sort.

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
Re: Sorting an array
by jdporter (Canon) on Feb 15, 2003 at 03:25 UTC
    I like to let perl manage the list of indices for me.
    my @month_names = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov De +c ); my @months = map { $_+1 } 0..$#month_names; my %labels; @labels{ @months } = @month_names; popup_menu( -values => \@months, -labels => \%labels, );

    jdporter
    The 6th Rule of Perl Club is -- There is no Rule #6.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (12)
As of 2014-08-20 13:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (114 votes), past polls