Perl: How to declare empty refs arrays in a new hash?

I have strict warnings and warnings, but he continues to complain about the initialization of the following line:

$hash{$key} = ($row, [], [], [], ''); 

He warns of this separate line:

 "Useless use of private variable in void context" "Useless use of anonymous list ([]) in void context" (3 times) 

I fill in the data later, but I want indexes 1, 2, 3 to be arrays references, and index 4 to be a string. I review and fill out the following data:

 $hash{$key}->[1]->[3] = 'Data'; $hash{$key}->[4] = $hash{$key}->[4] . 'More Data'; 

Obviously, I am doing something wrong, but I'm not quite sure how to do it right. (In addition, I know that this last line is redundant, can it be better summarized as well?)

+6
source share
3 answers

Hash elements can only be scalars, so you need to change your purpose to use the anonymous array constructor instead of parens:

 $hash{$key} = [$row, [], [], [], '']; 

See perldsc for details .

Line:

 $hash{$key}->[4] = $hash{$key}->[4] . 'More Data'; 

can write:

 $hash{$key}->[4] .= 'More Data'; 

And finally, if they don’t like you, the characters -> implicit between index delimiters, so $hash{$key}->[1]->[3] means the same as $hash{$key}[1][3]

+12
source

I'm not quite sure what you are trying to do, but if you want to assign an array to a scalar value, you need to use brackets to create an anonymous array:

 $hash{$key} = [$row, [], [], [], '']; 

In your case, what you are trying to do is interpreted as follows:

 $row, [], [], []; $hash{$key} = ''; 

Because you cannot assign a list of values ​​to a scalar (single value variable). However, as we did above, you can assign a reference to an anonymous array containing a list of values ​​for the scalar.

+3
source

You almost got it.

Remember that each hash and array value must be a scalar, so if you need a hash of arrays, you need to assign an array reference to your hash key. So:

 $hash{$key} = [ $row, [], [], [], '' ]; 

- Is this what you want.

+2
source

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


All Articles