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:$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;
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. Thankswhile (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} } } }
In reply to loops & sql results storage by breal
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |