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

I have a complicated problem to even explain, but I have posted a sample of the code to make my question clear or at least I am trying to.
All that I am trying to do is after I get a result back from a DB, I am storing this variable into an array:
push @array,$N_NUM;
And after sending this array to a sub routine. The sub will get all the values in this array and run another query to a DB, and for each value found it should return a FOUND or NOT FOUND back inside the same while loop where the push array was, because the rest of the code prints a lot of stuff to the browser. And doing it like in my code sample isn't working at all.
Would be very nice if I could get some feed back on it,
Thank you all!

Here is the sample code for my problem:

#More code is here, this is just to explain my problem... $sth->execute() || die $sth->errstr; while (my $pointer = $sth->fetchrow_hashref){ $N_NUM = &cleaner ($pointer->{'number'}); push @array,$N_NUM; &total(@array); #Print HTML print "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpaddin +g=\"0\"> <tr> <td width=\"15\">&nbsp;</td> <td class=\"textgreyb\">Products</td>"; # My problem is here cause I can only get the value of this varialbe $ +results when I have send the array @array to the SUB TOTAL + &total; print " <td class=\"textgreyb\">$results</td> </tr>"; } End while sub total { my $all_info = shift (@_); # gets values passed by @array, it has all +the Numbers found on DB my @info = split/ /,"$all_info"; # It checks a DB table and if the number is found here it should send +back a value of "FOUND" or "NOT FOUND" code would go here getting the values in here is not a problem... ok, let say that my final result is; my $results="FOUND"; return; }

Replies are listed 'Best First'.
Re: Sub routine processing issue, HELP!!!!!
by dragonchild (Archbishop) on Dec 08, 2004 at 20:59 UTC
    Write some non-code English, describing what you think this code should do. Then, slowly convert that English, over a number of steps, into code and see if you get to where you are.

    I think your main problem is that you're not understanding arrays and how they are passed in. Specifically, you pass an array to total(), but you seem to think it will be passed to total() as a space-delimited string.

    Furthermore, I would strongly suggest using something like HTML::Template.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Sub routine processing issue, HELP!!!!!
by Joost (Canon) on Dec 08, 2004 at 20:42 UTC
      Why don't you just take advantage of auto-vivification to generate hash keys and returning the value from your subroutine?

      Something like:

      sub total { my ($N_NUM) = @_; # expression is obviously pseudo-code if ($N_NUM is in database) { return 'FOUND'; } return 'NOT FOUND'; } my %dblist; $dblist{"$N_NUM"} = total($N_NUM); foreach my $item (sort keys %dblist) { # print table row print $item; print $dblist{"$item"}; }
      (edit: didn't really understand the question the first time, and am still not sure if I understand it.)
      I didn't know about SHOUTING, but back to the code,
      I already tried to replace, &total(@array) with &total($N_NUM), but I will sometimes have more than one value, I think for that reason I do need to use what I have, like,
      push @array,$N_NUM;
Re: Sub routine processing issue, HELP!!!!!
by friedo (Prior) on Dec 08, 2004 at 20:47 UTC
    There's no need for you to be using & to call subroutines. You should only use that syntax if you know what it does.

    Further, you need to be using strict and warnings. Turning those pragmata on would have given you lots of information about why your code isn't working. You're passing an array to total(), but only shifting a single scalar ($all_info) out of it. You're then trying to split $total on spaces, but elements of the array are supposed to be numerical. This doesn't make much sense. I reccomend seeing the following before going any further.

    perlsub strict warnings

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Sub routine processing issue, HELP!!!!!
by nedals (Deacon) on Dec 09, 2004 at 00:34 UTC

    There's a whole bunch of problems here. Apart from those already mentioned, do you really mean to put each result into a seperate table with no </table> tag?

    ..... ## Rather then getting '$all_info' with one fetch, then getting a N_NU +M with a seperate fetch, do it all at once. ## (assuming you are using two tables). Now you can skip the sub all t +ogether. my $tablerows = ""; ## Build the table row data, then add it to print +.... my $sth = $dhb->prepare("SELECT a.product, b.number FROM tableA a, tab +leB b WHERE a.id = b.id"); ## or whatever $sth->execute(); $sth->bind_columns(\my($product,$number)); ## use bind_columns while ($sth->fetch()) { my $found = ($number) ? 'FOUND' : 'NOT FOUND'; $tablerows .= qq{<tr><td width="15">&nbsp;</td><td class="textgrey +b">$product</td><td class="textgreyb">$found</td></tr>\n}; } ## Using here-is print <<HTML; <table width="100%" border="0" cellspacing="0" cellpadding="0"> $tablerows </table> HTML ....

    You might want to read up on HTML::Template, as suggested earlier, for another way to output out the HTML