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

I've recently started teaching myself CGI.pm using Stein's guide.

My question has to do with hash sorting. I'm doing something similar to the following example:

print $query->scrolling_list(-name=>'list_name',-values=>['eenie','meenie','minie','moe'],-default=>['eenie','moe'],-size=>5,-multiple=>'true',-labels=>\%labels);
What I'd like to know if there is any possible way to sort the %labels hash, or cause the scrolling_list routine to sort the hash so that my list box is not so random?

-naChoZ-

Replies are listed 'Best First'.
Re: CGI.pm - scrolling_list and %labels hash
by blakem (Monsignor) on Feb 14, 2002 at 23:31 UTC
    -labels simply provides extra information for each of the ordered -values. Therefore, the order of %labels isn't important, but the order of ['eenie','meenie','minie','moe'] passed to -values is.

    -Blake

      DOH...

      Thanx, that did the trick.

      naChoZ

Re: CGI.pm - scrolling_list and %labels hash
by rjray (Chaplain) on Feb 15, 2002 at 00:17 UTC

    To provide a little more specifics over the previous answer :-), it's hard to say without seeing your code, but I'm going to hazard a guess and say that you are providing the text values with something like:

    -values => [ keys %hash ]

    That is, you may have assigned the keys to an array, or maybe you just passed -labels and let it take the values from there. What you need to do is sort that list, as CGI will treat the list as an ordered set, and preserve the ordering you give it:

    -values => [ sort keys %hash ]

    If you aren't using the Official Guide to Programming with CGI.pm, I highly recommend getting a copy. Lincoln writes his documentation as well as he writes his code. And there are plentiful examples that are explained block-by-block.

    --rjray

      I spoke too soon, not quite there.

      What I've got is a hash where they key is a record number and the value is text. I want to pass the keys as -values, but sorted by the hash values. Here's my code... no laughing please....

      my %restaurants=&queryrestaurants; my @restaurantvalues=keys(%restaurants); print start_multipart_form(); print center(scrolling_list(-name=>'restauranttitle',-values=> +[@restaurantvalues],-default=>undef,size=>14,-multiple=>'false',-labe +ls=>\%restaurants),submit(-name=>'job',-value=>'edit'),submit(-name=> +'job',-value=>'new')); print endform; sub queryrestaurants { my (%restaurants,$restaurant,$address1,$address2,$zipcode,$phonenu +mber,$record); use DBI; my $dbh = DBI->connect("DBI:mysql:dbname:$sqlserver:3306","log +in","passwd"); my $sql_statement = "SELECT \* FROM location order by restaura +nt"; my $sth = $dbh->prepare($sql_statement); $sth->execute() or die "Can't execute SQL statement : $dbh->er +rstr"; $sth->bind_columns(undef, \$restaurant,\$address1,\$address2,\ +$zipcode,\$phonenumber,\$record); my $row; while ($row = $sth->fetchrow_arrayref) { $restaurants{$row->[5]}=$row->[0] . " " . $row->[1] . " \/ + " . $row->[3]; } $sth->finish; $dbh->disconnect; return %restaurants; }
      I know the $restaurants{$row->[5]}=$row->[0] . " " . $row->[1] . " \/ " . $row->[3]; is a little convoluted, but I wanted to end up with:

      <option value="42">Some Restaurant 200 Main St / 04444</option>
Re: CGI.pm - scrolling_list and %labels hash
by shotgunefx (Parson) on Feb 15, 2002 at 10:11 UTC
    Use Tie::SortHash for %labels.

    -Lee

    "To be civilized is to deny one's nature."