Michael Peppler
Sybase Consulting
Sybase on Linux
Install Guide for Sybase on Linux
General Sybase Resources
General Perl Resources
BCP Tool
Bug Tracker
Mailing List Archive
Downloads Directory
Sybase on Linux FAQ
Sybperl FAQ
Michael Peppler's resume

sybperl-l Archive

Up    Prev    Next    

From: Michael Peppler <mpeppler at MBAY dot NET>
Subject: Re: any way to get the # of columns returned
Date: Nov 3 1997 4:48PM

Raja Sambamurty wrote:
> Hi,
> Using sybperl, I very often do the following
> @retarr = $dbfh->sql ("select * from table");
> Now I can do a print $#retarr to find out the number of rows returned,
> but what I need to know is the number of columns. The reason I need to
> know this is to do a print stmt as in
> print "$retarr[0][1]\n";
>                ^^^^

You can use scalar(@{$retarr[0]}) to find out how many elements
are in the first row.

> to determine how far to go with the subscripts. Is there a better way of
> doing this ? I guess I could always work around with a
> foreach $i (0..$#retarr) {
>  my $j=0;
>  while ( defined $retarr[$i][$j] ) {
>     print "$retarr[$i][$j]";
>     $j++;
>  }
>  print "\n";
> }

That may bite you :-)

Remember that a Sybase NULL value is returned as the perl 'undef'
value, so if you have any NULL values in your result set the
while loop will end prematurely.

A better way to doe that would be something like

my ($i, $row);

foreach $row (@retarr) {
    for ($i = 0; $i < scalar(@$row); ++$i) {
         print "$$row[$i]";

It's a lot easier to work with references than with multi-dimensional
arrays in certain situations....

Michael Peppler       -||-  Data Migrations Inc.  -||-