Link to home
Start Free TrialLog in
Avatar of Dracan
Dracan

asked on

Not a HASH reference

Hi.  I'm getting a Perl error which I'm having trouble solving.  I've cut-down my Perl script to just a testcase of the problem (see attached code).  The final for-loop generates a Perl error saying "Not a HASH reference".  So something's going wrong inbetween copying the array-of-hashes to the main hash, and copying it back out again.  It's probably easier to look at the comments in the testcase code to see what I'm doing.

Thanks for any help with this,
Dan.

#!/usr/bin/perl
use strict;
use warnings;

# Create an array of hashes.

my @chapter_array;

my %chapter1 = ('title' => 'Chapter 1', 'chapter_index' => '1');
my %chapter2 = ('title' => 'Chapter 2', 'chapter_index' => '2');

push(@chapter_array, \%chapter1);
push(@chapter_array, \%chapter2);

# Add chapter array (array of hashes) to a new "main" hash.
my %options = ('chapters' => \@chapter_array);

# Note that this works as expected:
foreach(@chapter_array)
{
	print $_->{'title'} . "\n";
}

##################################################
##################################################
##################################################

# Get the chapters (array of hashes) from our "main" hash.
my @chapters = \$options{'chapters'};

# This doesn't work.
foreach(@chapters)
{
	print $_->{'title'} . "\n"; # This fails with "Not a HASH reference"
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of zlobcho
zlobcho
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dracan
Dracan

ASKER

Great!  Thanks for that :-)