Dear Monks, OK, I have an SQL query that returns some rows from a mysql table. I then apply a regex to search for & display specific titles... code is as follows:
$sth=$dbh->prepare($sql); $sth->execute() || die "Could not execute SQL statement... maybe invalid syntax?"; #show the results of the regex query in a table print "<br>Search Results:<br>". "<table border=1>"; while (my @array = $sth->fetchrow_array()){ print "<tr>"; foreach my $elem (@array){ #search & display the first element in the array for the r +egex $searchTitle=$array[0]; if ($searchTitle=~/(?=.*pr\w{4}m)(?=(?=\w*w\w*)(?=\w*b\w*) +\w+\b)/){ print "<td>"; print "$elem\n"; print "<td>"; } } } print "</table>"; $sth->finish; $dbh->disconnect;
This is held in a subroutine that when called, displays the results in a browser. Cue my problem... I need to perform another search on the results from this search. How do I save the results of the $searchTitle regex to a variable that I can use in another subroutine? I imagine theres two ways - printing to a log file that I can read back later on, or saving to a variable that I can pass to the other sub. Ive tried the following:
while (my @array = $sth->fetchrow_array()){ print "<tr>"; foreach my $elem (@array){ #search & display the first element in the array for the r +egex $searchTitle=$array[0]; if ($searchTitle=~/(?=.*pr\w{4}m)(?=(?=\w*w\w*)(?=\w*b\w*) +\w+\b)/){ print "<td>"; print "$elem\n"; print "<td>"; #SAVE OUTPUT FOR SEARCH 3: #WARNING: This prints out the title element n times where +n = number # of columns in the SQL search unless (open (LOGFILE, ">>//applications/mamp/logs/search1 +results")) {print "Failed to open search1results log!";exit} flock (LOGFILE, LOCK_EX); print LOGFILE "$searchTitle\n"; unless (close (LOGFILE)) {print "Failed to close search1results log!";exit} } } }
But as I indicated in the comments, this repeats every line returned once for every column of the table that im searching on. Was hoping theres an easier way to stick the $searchTitle results in an array that can be passed to another sub, but im not too familiar with passing variables between subroutines yet & don't know how to do it (ive read some tutorials but I just don't get how to apply them in my code above where ive got a regex searching on the first element of an array..). Any help or pointers greatly appreciated. In need of some divine intervention. Thanks

In reply to loops & sql results storage by breal

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.