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

Hi monks, I could use your help with this: I have my own function for retrieving the db data with fetchall_arrayref and I am building my UI around it with CGI.pm . Now when I want to use the result of fetchall_arrayref in
my $res=$db->select('SELECT name, sname FROM users'); #now I would assume it will only use "name" column for values of the p +opmenu function popupmenu(-values=>$res->[0])

what it does tho is that it only uses the first line! not the first column. How do I make it use the first column?! kind regards

Replies are listed 'Best First'.
Re: using fetchall_arrayref in popupmenu
by roboticus (Chancellor) on Feb 11, 2011 at 11:15 UTC

    kosta:

    It used the first line because that's what you gave it. The arrayref returned from (presumably DBI) is an arrayref with each row being an arrayref containing one row of the data. So you need to create an arrayref with one row containing all your data items. One way to do it would be to make a hash slice in your fetchall_arrayref call, then you can pass either the keys or values list to popupmenu.

    Alternatively, you can build a new arrayref that holds only the values in the first column, something like this:

    my @tmp=map { $_->[0] } @$res;

    which is roughly the same as:

    my @tmp=(); push @tmp, $_->[0] for @$res;

    The first one, though can be used in a single line with the popupmenu call:

    popupmenu(-values=>[ map { $_->[0] } @$res ]);

    Note: I didn't test any of this, so you'll have to fix any syntax errors I made.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: using fetchall_arrayref in popupmenu
by locked_user sundialsvc4 (Abbot) on Feb 11, 2011 at 13:43 UTC

    FYI, especially if you are hosting under a shared-hosting arrangement, the total memory available to your program might be extremely small.   Using fetchall_... to fill a popup menu is likely to always be okay (unless your site becomes extremely popular...), but in the general case it is wise to code at all times as though memory were at an extreme premium.

      thanks guys that certainly helps... I really havent had much experience with multidimensional arrays in perl