On Aug 18, 7:58 pm, [EMAIL PROTECTED] (Paul Lalli) wrote:
snip
>
> Is that seriously from the book? UGH. Just declare $array_ref in the
> proper scope, and there's no need to take a reference to a dereference
> of the reference.
>
> while (my $array_ref = $sth1->fetchrow_arrayref) {
> push @stash, $array_ref;
>
> }
Great - this worked fine.
>
> In this specific example, "data" is the sixth column in the SELECT
> query. You would therefore access it by $array_ref->[5].
>
No luck here, I could not retrieve anything (example code below).
> I would generally recommend you use the fetchrrow_hashref method
> rather than fetchrow_arrayref, so that you can refer to the column by
> name. This will prevent bugs in your code if you later change the
> query to add or remove columns. With fetchrow_hashref, the return
> value is a reference to a hash rather than to an array:
>
> while (my $hash_ref = $sth1->fetchrow_hashref()) {
> print $hash_ref->{data} . "\n";
>
> }
I was able to replicate this and also do a push to an array, but
again, I could not retrieve rows.
> I agree with other posters - you should read:
> perldoc perlref
> perldoc perlreftut
> perldoc perllol
> perldoc perldsc
>
> Paul Lalli
Thanks to all who suggested these perldocs - the perlreftut and
perllol were especially helpful.
I've learned more about references and also about scoping of variables
- now I'm doing "use strict" and it took a while to make it work
here.
A side question: why is it necessary to declare "our @variable" more
than once in a program?
The main problem is that I still can't retrieve data at will.
See below:
First, as an array - I tried commented-out options one at a time.
Msg is error message from Perl.
$sth1->execute();
while (my $array_ref = $sth1->fetchrow_arrayref) {
push ((our @stash), $array_ref); ##copy array contents
}
#use Data::Dumper 'Dumper';
# print Dumper our @stash ; #Msg: $VAR19586 = $VAR1;
###dump stash contents
foreach my $array_ref (our @stash ) {
# print "Row: $array_ref->[5]\n"; #Msg:Use of uninitialized value in
concatenation
# print $array_ref->[5]; #Msg: Use of uninitialized value in
concatenation
print "Row: @$array_ref\n"; #works, prints entire rows (from book)
}
foreach my $row (our @stash) {
# print "DATA: $row->[5]\n"; #Msg: Use of uninitialized value in
concatenation or string
# print "DATA: @$row->[5]\n"; #Msg: Use of uninitialized value in join
or string
#}
Now as a hash, which is what I would prefer, for the reasons stated by
Paul.
$sth1->execute();
while (my $hash_ref = $sth1->fetchrow_hashref()) {
#print $hash_ref->{data} . "\n"; # works, prints data
push our @stash, [ %$hash_ref ]; #works, copies hash contents
}
#use Data::Dumper 'Dumper';
# print Dumper our @stash; #showed good results (see below)
foreach my $hash_ref (our @stash ) {
#print "Row: %$hash_ref\n"; #Msg: Row: %ARRAY(0x13f88d44)
#print "Row: $hash_ref\n"; #Msg: Row: %ARRAY(0x13f88d44)
#print %$hash_ref . "\n"; #Msg: 'Can't coerce array into hash at'
}
from hash Data Dumper:
$VAR790 = [
'time_stamp',
'Mon Mar 05 2007 11:07:11',
'message_event',
'MESSAGE OUT',
'message_no',
'12101589',
'data',
' MSH|^~\\&|HUB|HOSP.MED.XXXX.EDU|PM3.0|HOSP.MED.XXXX.EDU|200
',A|AA|9}|MESSAGE ACCEPTED||
'message_index',
'36314470'
];
$VAR791 = [
'time_stamp',
'Mon Mar 05 2007 11:07:11',
etc.....
What am I doing wrong?
Thanks for all the help.
Peter Link
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/