How can I use an existing array as a value in a hash in Perl?

I have an existing array that I want to add as a value to the hash. I know that you can use arrays as values, but you cannot assign an existing one. I basically want to go:

$hash{fieldName} = @myArray;

Only this obviously does not work! Help rate!

+3
source share
4 answers

You can only store scalar values ​​in hashes / arrays. You need to use:

$hash{fieldName} = \@myArray;

to save it, and:

my @myOtherArray = @{$hash{fieldName}};

to return it. It works with scalar requirement using array reference.

+12
source

And since no one mentioned this, your code looked like this:

  • ,

  • $hash{fieldName} (scalar @myarray)

+6

, , , \@myArray, [ @myArray ] ( ) dclone ( ).

$hash{fieldName} = \@myArray, $hash{fieldName}->[2] @myArray. @myArray , .

+5

, '\',

$hash{fieldName} = \@myArray

:

@{$hash{fieldName}}
+4

Source: https://habr.com/ru/post/1719167/


All Articles