简体   繁体   中英

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)?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM