quentinnuk
quentinnuk

Reputation: 335

Can't use string as an ARRAY ref while "strict refs"

I'm trying to dereference an array that is a value hash of a list element that is within an array so that I can manipulate and store it.

A sample of code is:

use strict;
use warnings;

my @array = qw(one two three four);
my @objects;

$objects[0]{"name"}="somestring";
$objects[0]{"value"}=@array;

print $objects[0]{"name"} . ": " . $objects[0]{"value"}[0];

print "\n";

When I try and run, I get:

Can't use string ("4") as an ARRAY ref while "strict refs" in use at listarray.pl line 11.

Is there a way to do what I am intending (and use a foreach to iterate the inner and outer array)?

Upvotes: 2

Views: 334

Answers (1)

toolic
toolic

Reputation: 62082

You should store the array as a reference:

$objects[0]{"value"} = \@array;

In your code, the @array was evaluated in scalar context, which returns the number of elements in the array (4).

Upvotes: 4

Related Questions