jean wrote:
Hello all,

Hello,

based on your comments I've changed the script and it's working fine. Thanks for all the input.

The double print was produced by:
        foreach (my @result = $sth->fetchrow_array) {
I've changed this into:
        my @result=$sth->fetchrow_array;
        foreach ($result[0]){

That will be a loop that loops through one item ever, even if the query returns a hundred rows...

         for my $res ($sth->fetchrow_array) {

         }

Also I've changed
        for ($i = 1; $i <= $n+1; ++$i ) {
into
        for my $i(1 .. $n) {

I've been looking at my variable declaration following Jeff's remarks. I thought my (@sku, @qty, @t_qty); could be palaced after for my $i(1 .. $n) { since I only need them in that part of the program. However when I do so I get the following error
        Use of uninitialized value in integer eq (==)
        at ./webstore-rijselect-7-wip.pl line 40.

What is line 40 that has an == in it?

Use of uninitialized value in string at ./webstore-rijselect-7-wip.pl line 53.

What is line 53?

any ideas?


my current code is
________________________________________
#!/usr/bin/perl
#####################################################
#           webstore-rijselect-6-werkt.pl           #
#####################################################
use warnings;
use strict;

use integer;

Why?

use DBI;

my ($dbh, $sth, $n, @sku, @qty, @t_qty);


Oi, no no no :) why why why? don't do that...

Part of your uninit warnings is having vars that shoud be scoped global...


$dbh = DBI->connect('dbi:mysql:DB','U','PW',  {

my $dbh
       PrintError => 1,
RaiseError =>1 });

no error chekcing, booooo, also undefined values possible


my $row_total = 'SELECT num FROM nra_slim';
$sth=$dbh->prepare($row_total);
$sth->execute;
$sth->fetchrow_array();
$n = $DBI::rows;

my $n = ...

and what id $DBI::rows is undef or 0 then the loop willbe 1 .. 0 or 1 .. undef ...

my $dbh = DBI->connect(...) or die 'Could not connect to DB: ' . DBI->errstr();

my ($row_total) = $dbh->selectrow_array('SELECT COUNT(*) FROM nra_slim');

for my $i(1 .. $n) {

for my $counter(1 .. $row_total) {

Indent only 4 spaces...

        my $sku_compare = 'SELECT sku_srs, aantal FROM nra_slim WHERE num = ?';
my $sku_compare = 'SELECT sku_srs, aantal FROM nra_slim WHERE num = ' . $dbh->quote($counter);

        $sth=$dbh->prepare($sku_compare);
        $sth->execute($i);
        my @result = $sth->fetchrow_array;
        foreach ($result[0]) {

for my $record (@{ $dbh->selectall_arrayref($sku_compare) } ) {



        $sku[$i]=$result[0];
        $qty[$i]=$result[1];
        $t_qty[$i]=0;

A bit convoluted.....

Just Access sku_srs with $record->[0] and aantal with $record->[1]

Why copy them? you all ready have then in an array why put them in another. Be consistent and efficient.

The code below is totaly unreadable and could use some major improvements. get a copy of Damian Conway's Perl Best Practices book.

        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-1]",', totaalaantal is ', 
"$t_qty[$i-1]\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;
__________________________________________________


#!/usr/bin/perl

use strict;
use warnings;
use integers; # again, why?
use DBI;

my $dbh = DBI->connect(...) or die 'DB connection flopped: ' . DBI->errstr();

my ($row_total) = $dbh->selectrow_array('SELECT num FROM nra_slim');

# this query seems wrong, or at least confusing, are you trying to get a count, the highest value, the last record, the first record, etc ???

for my $num_field(1..$row_total) {
my $sku_compare = 'SELECT sku_srs, aantal FROM nra_slim WHERE num = ' . $dbh->quote($counter); # since its numeric just quote it, no need fothe placeholder, prepare execute dance...


    for my $record (@{ $dbh->selectall_arrayref($sku_compare) } ) {
        # do whatever it is you're trying to do here with $record
        # scope the vars right and make it readable
        print "sku_srs $record->[0]\n";
        print "aantal $record->[1]\n";
    }
}

--
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