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

I'm trying to populate a CGI scrolling list using the return from a database query. The relevant part of the code is:

my $doclist = $dbh->selectall_arrayref( q{SELECT name FROM products}); my $q = CGI->new; print $q->header, $q->start_html('Edit comments'), $q->h1('Edit Comments'); print scalar(@$doclist); print scrolling_list(-name => 'cboProj', -values => @$doclist);

This tells me that here are two values being returned (the correct number), but only the first appears in the list. I have tried various forms of dereferencing, but although some of these put two lines in the list, they are all of the form of a reference rather than the data that are in the database. I suspect I'm barking up the wrong tree with dereferencing, but I can't find any suggestions of what to try beyond the CGI documentation, which puts hard coded values in by using square brackets (one of the things that gives two lines of the ARRAY(0x*) type). Can anyone point me at some decent documentation for this, please?

Regards,

John Davies

Replies are listed 'Best First'.
Re: Populating a CGI scrolling list from a database
by moritz (Cardinal) on Feb 15, 2011 at 16:13 UTC

    selectall_arrayref returns an array ref of rows, and each row is an array ref again, so it looks like this:

    my $doclist = [['prod1'], ['prod2']];

    You can remove remove these easily:

    my @flat_doclist = map $_->[0], @$doclist; # and then later: print scrolling_list(-name => 'cboProj', -values => \@flat_doclist);

    See also: perllol, perldata, perlreftut.

      Thank you, your advice works perfectly. I was misreading the output I'd got from Data::Dumper and not getting the point that I was getting an AoA. Now that I understand your answer, I understand the question I should have been asking myself (I'm sure that 42 comes in here somewhere (-: ).

      Regards,

      John Davies

Re: Populating a CGI scrolling list from a database
by Anonyrnous Monk (Hermit) on Feb 15, 2011 at 16:14 UTC

    As per the docs, the values argument should be an array reference, i.e. in your case

    ... print scrolling_list(-name => 'cboProj', -values => $doclist);

    Update: though, if @$doclist isn't a flat list of names (as moritz points out), this won't work either...  My apologies, spoke too soon — just ignore my post.