Kyle R . Burton on Thu, 23 May 2002 17:27:34 -0400


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Perl question - references


> I've been working on some perl code and I'm having trouble with
> references.  This situation is this:
> 
> I've got a list of batch numbers.  Unfortunately, there are some
> missing batches.  So, I may have batches (1, 2, 3, 5).
> 
> Now, if I am at the last batch (5) and I want to go back by one batch
> then I have to go to 3, not 4.  Simple subtraction won't work.
> 
> Here is the code that gets the array reference of batches from the
> database:
> 
> 1   $stH0 = $dbH0->prepare(
>            " SELECT batch_number " . 
>            "   FROM process_history " .
>            "  WHERE customer = '" . $CustomerNum . "'" .
>            "    AND batch_number > " . $LowLimitBatch .
>            "  ORDER BY batch_number " ) ;
> 2   $stH0->execute ;
> 3   ($BatchArray) = $stH0->fetchall_arrayref ;
>
> Here, I want to print out the array index and the contents of that 
> array element:
> 
> 4   for (my $i = 0 ; $i < (@$BatchArray) ; $i++  ) {
> 5       print "i: $i\tvalue: @$BatchArray[$i])\n" ;
> 6   }


I think fetchall_arrayref returns an array of arrays:

   print "i: $i\tvalue: ",$BatchArray[$i]->[0],"\n";

Since you only selected 1 column, there will only be 1 value in the
inner array.

DBI aslo has a selectall_arrayref (similar to fetchall_arrayref, but it
does the prepare for you), and selectcol_arrayref (wich gives you a 1
dimensional array):

  my $sql = q{
          SELECT batch_number
            FROM process_history
           WHERE customer = ?
             AND batch_number > ?
           ORDER BY batch_number
  };
  my $resultSet = $dbH0->selectcol_arrayref(
    $sql,
    undef,         # no statement params
    $CustomerNum,  # bind var1
    $LowLimitBatch # bind var2
  );

  unless( $resultSet ) {
    confess "Error executing sql: '$sql' ",$dbH0->errstr(),"\n";
  }

  for( my $i = 0; $i < @{$resultSet}; ++$i ) {
     print "i: $i\tvalue: $resultSet->[$i]\n" ;
  }

If you use selectall_arrayref, then it's a 2 dimensional array:

  for( my $i = 0; $i < @{$resultSet}; ++$i ) {
     print "i: $i\tvalue: $resultSet->[$i]->[0]\n" ;
  }

HTH

Kyle

> As you can see on line 5, I use @$BatchArray[$] to get the element.
> Unfortunately, I see this when I run it:
> 
> i: 1    value: ARRAY(0x8202900))
> i: 2    value: ARRAY(0x8202924))
> i: 3    value: ARRAY(0x820cab4))
> i: 4    value: ARRAY(0x820cad8))
> i: 5    value: ARRAY(0x820cafc))
> i: 6    value: ARRAY(0x820cb20))
> 
> I would have expected this....
> 
> i:0     value: 1
> i:1     value: 2
> i:2     value: 3
> i:3     value: 5
> i:4     value: 6
> 
> I even tried $BatchArray->[$i]
> no luck.
> 
> Any help is greatly appreciated.
> 
> 
> Eric
> -- 
> # Eric Allan Lucas 
> #-------------------
> # Support Ken Krawchuk - Libertarian for Governor of Pennsylvania!
> # Find out how YOU can help -> http://www.kenk.org     
> #------------------- 
> **Majordomo list services provided by PANIX <URL:http://www.panix.com>**
> **To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**

-- 

------------------------------------------------------------------------------
Wisdom and Compassion are inseparable.
        -- Christmas Humphreys
mortis@voicenet.com                            http://www.voicenet.com/~mortis
------------------------------------------------------------------------------
**Majordomo list services provided by PANIX <URL:http://www.panix.com>**
**To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**