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

Hello, I'm trying to figure out how to use the "matching options" in the following script over and over again in a couple different areas on a form...
open(IMGDB, "$img_db"); # Open db for reading @imgarray=<IMGDB>; close (IMGDB); foreach my $imgline (@imgarray) { if ($imgline =~ /\Q$search\E/i) { my ($img_name,$img_desc) = split /\|/, $imgline; print <<End_of_head; <option value='$img_name'>$img_desc</option> End_of_head } }
Would I need to push this into an array? If so, how could I accomplish this? Thanks for any help

Replies are listed 'Best First'.
Re: Array Question
by sacked (Hermit) on Jun 08, 2004 at 15:27 UTC
    perldoc -f push will show the documentation for the push function.

    Here is an example of how you could save your "matching options" in an array for use in other parts of your program:
    my @opts; if ($imgline =~ /\Q$search\E/i) { my ($img_name,$img_desc) = split /\|/, $imgline; + push @opts => qq{<option value="$img_name">$img_desc</option>}; } + # ... later, print out HTML select list print "<select name=foo>\n", join( "\n" => @opts ), "\n</select>";

    --sacked
      Hi Sacked, That was exactly what I was looking for. Thank you for the help!
Re: Array Question
by Plankton (Vicar) on Jun 08, 2004 at 15:16 UTC
    Would I need to push this into an array?
    What is this?

    If so, how could I accomplish this?
    Are you sure you mean this and not that?

    The code you posted looks fine. I just don't understand the rest of your post.

    Plankton: 1% Evil, 99% Hot Gas.
Re: Array Question
by BrowserUk (Patriarch) on Jun 08, 2004 at 15:22 UTC
    open(IMGDB, "$img_db"); # Open db for reading @imgarray=<IMGDB>; close (IMGDB); my @matchingOptions = grep{ /\Q$search\E/i }, @imgarray; foreach my $imgline (@matchingOption) { my ($img_name,$img_desc) = split /\|/, $imgline; print <<End_of_head; <option value='$img_name'>$img_desc</option> End_of_head } } ... for my $imgline ( @matchingOptions ) { # Do things; }

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
Re: Array Question
by pelagic (Priest) on Jun 08, 2004 at 15:22 UTC
    What is your problem actually. What does your code do and what should it do?

    pelagic
      if you look lisaw writeups, people keep asking, no answer
Re: Array Question
by TomDLux (Vicar) on Jun 09, 2004 at 02:56 UTC

    Instead of putting arbitrary HTML text into strings, you might consider using CGI routines. better yet, use HTML::Template or Template::Toolkit to separate the code fromt eh web page.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA