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

I am trying to figure out how to loop an entry from a flat database to be used in a drop-down selection. So, if my database looks something like this:
company1|www.company1.com|absolute_path1 company2|www.company2.com|absolute_path2 company3|www.company3.com|absolute_path3 etc...
I only want the "company" pulled from the database and listed in the drop-down. Here's what I've done so far:
#!perl use strict; use CGI; $|=1; my $db="g:/Scripts/Indigo/cgi-bin/company.db"; my $query=new CGI; my $g_debug=0; my $path='g:/Scripts/Indigo/cgi-bin/'; my $rc=0; my ($u,$p); my $userid=$query->param('userid'); my $url=$query->param('url'); # open the text database unless(open(PFD,$db)) { &printError("Could not open user database"); return; } # first check if user exist $path=''; while (<PFD>) { chomp; ($u,$p,$path)=split('\|',$_);###$u loop?? how? if ($userid eq $u) { $rc=1; last; } } close(PFD); print $query->popup_menu(-name=>'userid', -values=>[$u]);## $u needs loop to pull al +l company entries from $db
Thank you all for any help that you can give me.

Replies are listed 'Best First'.
Re: Drop Down Selection #2
by suaveant (Parson) on May 08, 2001 at 21:28 UTC
    First of all, return doesn't work when you are not in a subroutine, so you probably want exit(1);

    You are already in a loop for $u... the while is a loop, what you should do is create an array, and push values of $u on it as you get them, so you would do

    push @users, $u;
    in your loop, that adds values of $u one by one to the array, appending them to the end. Then your call would look like
    print $query->popup_menu(-name=>'userid', -values=>\@users);## $u needs loop to pu +ll all company entries from $db
    at least, I think that is what you are asking for?
                    - Ant
      Ant, Thank you for your response to my question. I am still fairly new to CGI scripting, which is why I need to ask the following question: How should the script look with the push added? I've tried multiple ways of inserting the push but I keep getting a 500-Internal Error. I understand this completely:
      print $query->popup_menu(-name=>'userid', -values=>\@users);
      But the part that has me stiffled, is this:
      # open the text database unless(open(PFD,$db)) { &printError("Could not open user database"); exit(1); } # first check if user exist $path=''; while (<PFD>) { chomp; ($u,$p,$path)=split('\|',$_);###$u loop?? how? if ($userid eq $u) { $rc=1; last; } } close(PFD);
      Thank you so much
        # open the text database unless(open(PFD,$db)) { &printError("Could not open user database"); exit(1); } my @users; # first check if user exist $path=''; while (<PFD>) { chomp; ($u,$p,$path)=split('\|',$_);###$u loop?? how? push @users, $u; if ($userid eq $u) { $rc=1; last; } } close(PFD); print $query->popup_menu(-name=>'userid', -values=>\@users);
        That should work, but in your code you do a last if $userid eq $u, which means @users will store all the users in the list until the one that matches $userid... not sure if that is what you want.
                        - Ant