in reply to Re: Sorting an array II
in thread Sorting an array II

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

Replies are listed 'Best First'.
Re: Re: Re: Sorting an array II
by Chady (Priest) on Mar 03, 2003 at 14:34 UTC
    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/