On Nov 8, jean said:

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]){

There's really no need for a 'foreach' loop there at all.

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

Just because you only use them inside that for loop doesn't mean they can be DECLARED in there. They must be declared OUTSIDe that loop because you need the arrays to be built up over time -- if you declared them inside the loop, they'd be reset to () for each value of $i.

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

use integer;
use DBI;

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

I'd love the array declarations a little below:

$dbh = DBI->connect('dbi:mysql:DB','U','PW',  {
      PrintError => 1,
      RaiseError =>1
});


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

Here is where I'd put them. Yes, it's the same scope, but declaring them here is a VISUAL tool to you and anyone else who sees your code.

  my (@sku, @qty, @t_qty);

for my $i(1 .. $n) {
        my $sku_compare = 'SELECT sku_srs, aantal FROM nra_slim WHERE num = ?';
        $sth=$dbh->prepare($sku_compare);

Those two lines above can go outside this loop; there's no need to prepare the same query over and over again.

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

Get rid of the line above (and its matching '}' below).

        $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";

I'd just print one string:

  print "$sku[$i], totaalaantal is $t_qty[$i]\n";

And so on, for your other print() statements. This is just so much easier to look at than a bunch of strings with different quotes and commas between them.

                        }
                }
                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";
                        }
                }
        }
}

I believe the } above is the one matching your foreach statement.

}

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

--
Jeff "japhy" Pinyan        %  How can we ever be the sold short or
RPI Acacia Brother #734    %  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %    -- Meister Eckhart

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