hello there,

Being new to PERL, I've found this very informative list and I've been playing 
around with PERL a bit. 

Right now I'm working on some code that picks up items from my database and 
calculates the total stock for identical items. I've created a small database 
to try out my script. 

Now I've probably made more stupid mistakes than anyone on this list has ever 
made. Also I bet there are different and more efficient ways to write code 
that performs this function. However my main problem is that the script does 
not do what I expect and I can't figure out why. In the output all the lines 
are printed twice, where I expect them to appear only once. The totals are 
calculated correctly.

So if anyone here can help me, that would be very much appreciated

thanks, jean


> HERE'S MY SCRIPT:
#!/usr/bin/perl
#####################################################
#           webstore-rijselect-5-werkt.pl           
#####################################################
use warnings;
use strict;

use integer;
use DBI;

my ($dbh, $sth, $sta, $sql_een, $sql_twee, $i, $n, @sku, @qty, @t_qty);



# create a statement in $sth
$dbh = DBI->connect('dbi:mysql:webstore-2','gabala','',  {
       PrintError => 1, ### rapporteer fouten via warn()
       RaiseError =>1  ### rapporteer fouten via die()
});



$sql_een = 'SELECT num FROM nra_produkten';
# prepare a SQL query with placeholder
$sth=$dbh->prepare($sql_een);
$sth->execute;
$sth->fetchrow_array();
$n = $DBI::rows;

for ($i = 1; $i <= $n+1; ++$i ) {
        $sql_twee = 'SELECT sku_srs, aantal FROM nra_slim WHERE num = ?';
        $sth=$dbh->prepare($sql_twee);
        $sth->bind_param(1, "$i");
        $sth->execute;

        foreach (my @result = $sth->fetchrow_array) {
        $sku[$i]=$result[0];
        $qty[$i]=$result[1];
        $t_qty[$i]=0;
        if ($i < 2) {
                $t_qty[$i]=$qty[$i];
                }
        else {
                if ($sku[$i]==$sku[$i-1]){
                        $t_qty[$i]= $t_qty[($i-1)] + $qty[$i];
                        if($i==$n){
                                print "$sku[$i]",', totaalaantal is ', 
"$t_qty[$i]\n";
                                }
                        }
                else {
                        $t_qty[$i]=$qty[$i];
                        if($i==$n){
                                print "$sku[$i]",', totaalaantal is ', 
"$t_qty[$i]\n";
                                }
                        else {  
                                print "$sku[$i-1]",', totaalaantal is ', 
"$t_qty[$i-1]\n";
                                }
                        }
                }
        }
}


$sth->finish;
$dbh->disconnect;

> HERE's MY OUTPUT
1989, totaalaantal is 5
1989, totaalaantal is 5
4121, totaalaantal is 1
4121, totaalaantal is 1
4122, totaalaantal is 1
4122, totaalaantal is 1
4123, totaalaantal is 2
4123, totaalaantal is 2
4959, totaalaantal is 11
4959, totaalaantal is 11
5337, totaalaantal is 25
5337, totaalaantal is 25
5338, totaalaantal is 1
5338, totaalaantal is 1
5938, totaalaantal is 10
5938, totaalaantal is 10
18164, totaalaantal is 1
18164, totaalaantal is 1
21505, totaalaantal is 7
21505, totaalaantal is 7
29765, totaalaantal is 1
29765, totaalaantal is 1

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to