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

Hi,Monks?!!!!
This code will bring back information from a database, and variable $line
will bring sometimes more than one result. How can I print this variable out side of
the while loop and print all the results coming back from the database.
How can I do that?
If I print variable $line out side of the while loop it will only print
the first result I know that is because there is no interaction on the variable to print all
the results found, somehow I know that there is a way to assign the results into an array
and dynamically out side be able to print everything found, I just can't remember how it is done
I thank you very much for this help!!!

$sth = $dbh->prepare($sql); $sth->execute() || die $sth->errstr; while ($pointer = $sth->fetchrow_hashref) { $line=$pointer->{'LINE'}; $ins =$pointer->{'INS#'}; $number =$pointer->{'NUMBER#'}; if($line_type) { $ result = "<td><a href=found.html?line=$line>Found $line</ +td><br>"; exit; } else { print "<META HTTP-EQUIV=\"refresh\" CONTENT=\"0; URL=notfou +nd.html\"> \n"; exit; } } } print $line;

Replies are listed 'Best First'.
Re: While Loop Problem!
by CombatSquirrel (Hermit) on Dec 12, 2003 at 15:40 UTC
    You never declare or initialize $line_type in your code, which looks a bit suspicious, because you check for definedness of it.
    To your question: You could just push the lines onto an array within the while loop.
    Example:
    #!perl use strict; use warnings; my @lines; for (qw/one two three/) { push @lines, $_; print "Found *** $_ *** $/"; } print "Lines: ", join " - ", @lines;

    Hope this helped,
    CombatSquirrel.
    Entropy is the tendency of everything going to hell.
Re: While Loop Problem!
by shockme (Chaplain) on Dec 12, 2003 at 15:43 UTC
    I'm not sure exactly what you're asking, but if it the problem is that you can't remember, it seems that a brief look through perldoc would've refreshed your memory.

    If I understand what you're wanting, this should get you going:

    $sth = $dbh->prepare($sql); $sth->execute() || die $sth->errstr; my @line; while ($pointer = $sth->fetchrow_hashref) { push @line, $pointer->{'LINE'}; $ins =$pointer->{'INS#'}; $number =$pointer->{'NUMBER#'}; if($line_type) { $result = "<td><a href=found.html?line=$line>Found $line</td><br +>"; exit; } else { print "<META HTTP-EQUIV=\"refresh\" CONTENT=\"0; URL=notfound.ht +ml\"> \n"; exit; } } print @line; # # or # foreach my $line (@line) # { # print "$line\n"; # }

    Update: CombatSquirrel beat me to the punch. I hate telephones.

    If things get any worse, I'll have to ask you to stop helping me.

Re: While Loop Problem! (architectural comment)
by talexb (Chancellor) on Dec 12, 2003 at 16:09 UTC

    This is an architectural comment -- so take it with a pinch of salt.

    You should think about separating the presentation layer (printing out HTML) from your business logic. In this example, I would have the subroutine (if that's what this fragment is) return $line, which would be either a valid value or undef. Up above, the caller routine would decide what to output.

    Following on that idea, think about using the excellent CGI module for outputting HTML using an OO approach rather than printing raw HTML.

    --t. alex
    Life is short: get busy!