On Aug 23, 7:13 pm, [EMAIL PROTECTED] wrote: > On Aug 18, 7:58 pm, [EMAIL PROTECTED] (Paul Lalli) wrote: > 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?
'our', like 'my', is lexically scoped. Its effects are terminated when the innermost enclosing block ends. So if you're using the same package variable in two different blocks, you have to use 'our' in each of them: package Foo; { our $bar; #for the duration of this scope, you can use $Foo::bar by just saying $bar } #now scope of 'our' has ended, must fully qualify $Foo::bar again { #new scope, must still say $Foo::bar our $bar; #Can now call $Foo:: bar as just $bar again } > 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; Your syntax for 'our' is confusing at best. Just declare it once, at the very top of your program. From the output however, it looks like you have one of the older versions of DBI that Randal mentioned in his post. That is, you cannot do what I suggested. You have to make an explicit copy. my @stash; #there's no reason this has to be 'our' to begin with. while (my $array_ref = $sth1->fetchrow_arrayref) { push @stash, [ @{$array_ref} ]; } print Dumper([EMAIL PROTECTED]); > ###dump stash contents > foreach my $array_ref (our @stash ) { Again. Stop using 'our' all over the place. Declare your variables once, at the beginning of your program, if it's being used throughout the program. > # 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) And what is the output of that? If $array_ref->[5] is undefined, then you do not have six elements in your result arrays, which means you're using a different query than the one you originally posted, and you'll have to adjust your code accordingly. > 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 I wouldn't say "works" here. I would say nothing more than "doesn't generate compiler errors". This doesn't do what you think it does. This creates a reference to an *array* that contains the keys and values of your hash. You don't want to do that. You want to create a reference to a *hash*. To do that, we use { } instead of [ ] push @stash, { %{$hash_ref} }; > > } > > #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' Right. All these messages are because you put array refs into @stash rather than hash refs. Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/