in reply to CGI.pm - scrolling_list and %labels hash

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

Replies are listed 'Best First'.
Re: Re: CGI.pm - scrolling_list and %labels hash
by naChoZ (Curate) on Feb 15, 2002 at 00:57 UTC
    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>
        Kanji? Former RSN Kanji? Cool bumping into you here.

        Thanx for the input. Didn't know you were a perl monk. :) I knew I'd find a use for that spaceship operator some day.

        -Andy