shockme has asked for the wisdom of the Perl Monks concerning the following question:

The purpose of the following code is to put (a|multiple) hash(es) into an array, and then retrieve one particular hash value from the array. This simplifies things with HTML::Template's TMPL_LOOP mechanism, especially when retrieving data from MySQL.

The following code produces this:

Global symbol "@returnArray" requires explicit package name at ./test. +pl line 16. Execution of ./test.pl aborted due to compilation errors.
So, I'm obviously missing the boat somewhere. I've checked out several threads on the subject and reviewed what I believe are the applicable perldocs, but I'm just not getting it. (Would anyone tell me if I was getting stupider?) I think I've stared at and studied it for so long, I've gone blind to the simplicity of the situation.

Thanks for any insight or thoughts you guys might have. I really think I'm overlooking something fairly simple, but its eluding me.

#!/usr/bin/perl -w use strict; use Data::Dumper; my %hash = { 'node_id' => '1', 'node_title' => 'whiskey', 'node_author' => 'otis', 'node_content' => 'the path to wisdom is littered with empty bottle +s', }; my $returnArray; push @{$returnArray}, \%hash; print Dumper(${$returnArray}[0]{node_content});

If things get any worse, I'll have to ask you to stop helping me.

Replies are listed 'Best First'.
Re: Modifying an Element in an Array of Hashes (boo)
by boo_radley (Parson) on Apr 18, 2002 at 02:02 UTC
    my %hash = { 'node_id' => '1', 'node_title' => 'whiskey', 'node_author' => 'otis', 'node_content' => 'the path to wisdom is littered with empty bottle +s', };
    This is a hashref, not a hash. I'm curious as to why you didn't get an error on this before the one on line 16. under activestate 5.6.1 build 631, once this is corrected (change braces to parens), it outputs
    $VAR1 = 'the path to wisdom is littered with empty bottles';
    update :
    my %hash = ( 'node_id' => '1', 'node_title' => 'whiskey', 'node_author' => 'otis', 'node_content' => 'the path to wisdom is littered with empty bottle +s', );
    This is a hash, see the parens? like my %hash=(), but a hashref uses braces : my $href={}
      Well, sure I see the parens - now that you've taken me by the back of the head and pointed my nose right at them. ;)

      I got so caught up in getting the data out that it never occurred to me that the problem might be how data was being put in. I guess I was so focused on the format of the print statement that I couldn't see anything else.

      Thanks, boo_radley!

      If things get any worse, I'll have to ask you to stop helping me.