Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Sorting an array II

by powerhouse (Friar)
on Mar 03, 2003 at 08:13 UTC ( [id://239969]=perlquestion: print w/replies, xml ) Need Help??

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

Is there a way to sort an array by the labels?

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

That is for numeric keys, but what if each "value" is a URL and the LABELS are numbers. Can I do something similar to this:

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

I tried that and got an error. I tried replacing labels with values in the -values line, but that did not do it either.

Any one know how to do it?

thx,
Richard

Replies are listed 'Best First'.
Re: Sorting an array II
by robartes (Priest) on Mar 03, 2003 at 09:41 UTC
    Um, an array does not have any labels. A hash has keys, which is what you're using in your examples. If you want to sort a hash by the keys, use your first example (if the keys are numerical). If you want to sort the values, alphanumerically, use straight sort (which uses the cmp operator, which sorts strings in ascending ASCII order (thus roughly alphabetic) ):
    my @sorted_values=sort values %hash;

    CU
    Robartes-

      And of course, if using the first example, substituting keys instead of labels in the -value line should stop the error.

        Stops the error but doesn't solve the problem.

        You may note that the first code snippet uses keys, which isn't what the OP wants...

Re: Sorting an array II
by Chady (Priest) on Mar 03, 2003 at 09:27 UTC
    you're close.
    that's values and not labels.
    -values=>[sort {$a <=> $b} values %array],

    Update: Ok, I missed the last line. the code should work, what kind of error do you get?


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
      Well, when I use values instead of keys, I get this(not an actual error):

      <select name="change_max" class="formfield" onchange="window.location= +(document.max.change_max.value)"> <option value="1">1</option> <option value="10">10</option> <option value="100">100</option> <option value="15">15</option> <option value="20">20</option> <option value="200">200</option> <option selected value="25">25</option> <option value="300">300</option> <option value="35">35</option> <option value="400">400</option> <option value="5">5</option> <option value="50">50</option> <option value="500">500</option> <option value="75">75</option> </select>

      That is not correct. Here is the way the values should be instead of the numbers:
      <select name="change_max" class="formfield" onchange="window.location= +(document.max.change_max.value)"> <option value="http://www.ourgiftcottage.com/index.cgi?pg=admin_main; +load=edit_items;l=0;max=1">1</option> <option value="http://www.ourgiftcottage.com/index.cgi?pg=admin_main; +load=edit_items;l=0;max=10">10</option> <option value="http://www.ourgiftcottage.com/index.cgi?pg=admin_main; +load=edit_items;l=0;max=100">100</option> <option value="http://www.ourgiftcottage.com/index.cgi?pg=admin_main; +load=edit_items;l=0;max=15">15</option> <option value="http://www.ourgiftcottage.com/index.cgi?pg=admin_main; +load=edit_items;l=0;max=20">20</option> <option value="http://www.ourgiftcottage.com/index.cgi?pg=admin_main; +load=edit_items;l=0;max=200">200</option> <option value="http://www.ourgiftcottage.com/index.cgi?pg=admin_main; +load=edit_items;l=0;max=25">25</option> <option value="http://www.ourgiftcottage.com/index.cgi?pg=admin_main; +load=edit_items;l=0;max=300">300</option> <option value="http://www.ourgiftcottage.com/index.cgi?pg=admin_main; +load=edit_items;l=0;max=35">35</option> <option value="http://www.ourgiftcottage.com/index.cgi?pg=admin_main; +load=edit_items;l=0;max=400">400</option> <option value="http://www.ourgiftcottage.com/index.cgi?pg=admin_main; +load=edit_items;l=0;max=5">5</option> <option value="http://www.ourgiftcottage.com/index.cgi?pg=admin_main; +load=edit_items;l=0;max=50">50</option> <option value="http://www.ourgiftcottage.com/index.cgi?pg=admin_main; +load=edit_items;l=0;max=500">500</option> <option value="http://www.ourgiftcottage.com/index.cgi?pg=admin_main; +load=edit_items;l=0;max=75">75</option> </select>

      But that is not in the correct order(I used keys).
      Even when I use values instead of keys, It's not in the order I would like...
      I want it in order 1-500.

      Any Easy way to do that, here is my method:
      %max_go_array = ( "$url?pg=$in{pg};load=$in{load};l=$in{l};max=1$include_sess_id" => "1" +, "$url?pg=$in{pg};load=$in{load};l=$in{l};max=5$include_sess_id" => "5" +, "$url?pg=$in{pg};load=$in{load};l=$in{l};max=10$include_sess_id" => "1 +0", "$url?pg=$in{pg};load=$in{load};l=$in{l};max=15$include_sess_id" => "1 +5", "$url?pg=$in{pg};load=$in{load};l=$in{l};max=20$include_sess_id" => "2 +0", "$url?pg=$in{pg};load=$in{load};l=$in{l};max=25$include_sess_id" => "2 +5", "$url?pg=$in{pg};load=$in{load};l=$in{l};max=35$include_sess_id" => "3 +5", "$url?pg=$in{pg};load=$in{load};l=$in{l};max=50$include_sess_id" => "5 +0", "$url?pg=$in{pg};load=$in{load};l=$in{l};max=75$include_sess_id" => "7 +5", "$url?pg=$in{pg};load=$in{load};l=$in{l};max=100$include_sess_id" => " +100", "$url?pg=$in{pg};load=$in{load};l=$in{l};max=200$include_sess_id" => " +200", "$url?pg=$in{pg};load=$in{load};l=$in{l};max=300$include_sess_id" => " +300", "$url?pg=$in{pg};load=$in{load};l=$in{l};max=400$include_sess_id" => " +400", "$url?pg=$in{pg};load=$in{load};l=$in{l};max=500$include_sess_id" => " +500" ); $change_max .= popup_menu(-name=>"change_max", -class=>"formfield", -values=> [sort {$a cmp $b} keys %max_go_array], -labels=> \%max_go_array, -class=>"formfield", -default=>$maxlimit_display, -onchange=>"window.location=(document.max.change_max.value)" );

      I would appreciate any advice to help me out...( I'm building this site for my Wife;o) )
      I want it to create the popup_menu in the order the array is listed.

      thx,
      Richard
        You're using cmp which compares strings, you need to compare numbers so you should use <=>
        This should do it for you
        my %hash = map { "$url?pg=$in{pg};load=$in{load};l=$in{l};max=$_$include_sess_id" , "$_" } (1, 5, 10, 15, 20, 25, 35, 50, 75, 100, 200, 300, 400, 500); $change_max .= popup_menu(-name=>"change_max", -class=>"formfield", -values=> [sort {$a <=> $b} keys %hash], -labels=> \%hash, -class=>"formfield", -default=>$maxlimit_display, -onchange=>"window.location=(document.max.change_max.val +ue)" );

        He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

        Chady | http://chady.net/
Re: Sorting an array II
by jsprat (Curate) on Mar 03, 2003 at 17:59 UTC
    What you are looking for (if I understand you right) is the keys of %array sorted by their values.

    Try this (see the -values line):

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

    BTW, why did you name a hash "%array" ;-)?

    Update: Here's a quick test:

    use CGI qw/:standard/; my %array = (a => 2, b=>1, c=>3); print popup_menu(-name=>'test', -values=>[sort {$array{$a} <=> $array{$b}} keys %array], -labels=>\%array) __END__ <select name="test"> <option value="b">1</option> <option value="a">2</option> <option value="c">3</option> </select>

      >BTW, why did you name a hash "%array"?

      Actually I did not, I just put that in the code I put on here, as it was shorter ;o)

      Here is what the real name is: max_go_array ( This site is pretty big, with tons of variables, I put the word array at the end so I know what it is, and where to look for it :o) )

      Thank you, this is what worked:
      {$max_go_array{$a} <=> $max_go_array{$b}}

      Putting that in the popup_menu worked. Thank you, VERY much!!


      thx,
      Richard

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-23 08:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found