Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Array references

Posted by Milo (Milo), 1 May 2003
I'd be grateful for any suggestions with some funny business that's happening with array references. I'm trying to extract whole rows from a postgresql database.
With my record, it's probably a silly and obvious mistake, but here it is:

$sth = $dbh->prepare($sql_string);
$sth->execute or die "Cannot execute: " . $sth->errstr();
my @returned=();
while (@row = $sth->fetchrow_array)
{
     push (@returned, \@row);
}
$sth->finish;
print "RET: @returned\n";
return @returned;

...fills up @returned with a load of references, as one may expect. However, when I return to the main body of the program and try access the contents of these arrays, e.g. with this:

foreach my $ret (@returned)
{
     print "R1: $ret\n";
     print "R2: " . @$ret . "\n";
}

...I get this:

R1: ARRAY(0x81cd2c
R2: 0

&c.
Any suggestions would be welcome!
Many thanks,
Milo Thurston.


Posted by admin (Graham Ellis), 1 May 2003
Hi, Milo ...

Two things ...

Firstly, you have not declared @row to be a my variable each time through the loop of fetchrow_array calls, so you're repeatedly pushing the SAME address onto the stack in @returned.   The very last time at the while statement,  an empty list is assigned to @row as there's nothing left for fetchrow_array to get from the result set.  Conculsion - @returned is a long list of pointers to the same null list!

Second, the . operator is used to concatanate strings and it works in a scalar context.  If you concatante a list in this way, it will assume you mean the length of the list which (as the lists are all empty because of item 1 above) is zero.

Solution:
      a) MY @row
      b) "R2: @$ret\n"

Sample code showing the second problem solved:

Code:
#!/usr/bin/perl

@sample = ([10,20,30],[40,50,60],[70,80,90,100]);

foreach $row (@sample) {
       print "$row\n";
       print " ... @$row ... \n";
       print " Hello ".@$row." .... \n";
       }



which runs as

Code:
[localhost:~/may03] graham% ./refref
ARRAY(0x6410)
... 10 20 30 ...
Hello 3 ....
ARRAY(0x11c8c)
... 40 50 60 ...
Hello 3 ....
ARRAY(0x11cc8)
... 70 80 90 100 ...
Hello 4 ....
[localhost:~/may03] graham%





Posted by Milo (Milo), 2 May 2003
Firstly, you have not declared @row to be a my variable each time through the loop of fetchrow_array calls, so you're repeatedly pushing the SAME address onto the stack in @returned.

Second, the . operator is used to concatanate strings and it works in a scalar context.

Thanks, Graham.
As I thought, they were silly mistakes.
Milo.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho