Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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 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

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


Replies are listed 'Best First'.
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: (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

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

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

      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)


        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

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, );

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://235464]
Approved by newrisedesigns
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (1)
As of 2018-02-20 00:29 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (266 votes). Check out past polls.