hashref

Syntax error in hashref lookup, can not see why

你离开我真会死。 提交于 2021-02-09 07:25:58
问题 perl -E 'say for map s/(æ|ø|å)/ {qw(æ ae ø oe å aa)}->{$1}/ger, qw(rød gul blå)' perl -E 'say for map s/(æ|ø|å)/"".{qw(æ ae ø oe å aa)}->{$1}/ger, qw(rød gul blå)' The first line above gives me syntax error at -e line 1, near "}->" but the second prints roed , gul and blaa as expected. Is this a weakness of the compiler or are there some reason for it that I can't see? I tested and got this behaviour in versions 5.10, 5.22 and 5.26. 回答1: The {...} are interpreted as a BLOCK, not a hashref. We

How to set a list of scalars from a perl hash ref?

那年仲夏 提交于 2020-01-30 08:04:40
问题 How do I set a list of scalars from a perl hash? use strict; my $my_hash = { field1=>'val1', field2=>'val2', field3=>'val3', }; my ($field1,$field2,$field3) = %{$my_hash}{qw(field1 field2 field3)}; print "field1=$field1\nfield2=$field2\nfield3=$field3\n"; 回答1: You're looking for a hash slice which in your case would look like this: my ($field1,$field2,$field3) = @{$my_hash}{qw(field1 field2 field3)}; or like this: my ($field1,$field2,$field3) = @$my_hash{qw(field1 field2 field3)}; If we

How to set a list of scalars from a perl hash ref?

心已入冬 提交于 2020-01-30 08:03:40
问题 How do I set a list of scalars from a perl hash? use strict; my $my_hash = { field1=>'val1', field2=>'val2', field3=>'val3', }; my ($field1,$field2,$field3) = %{$my_hash}{qw(field1 field2 field3)}; print "field1=$field1\nfield2=$field2\nfield3=$field3\n"; 回答1: You're looking for a hash slice which in your case would look like this: my ($field1,$field2,$field3) = @{$my_hash}{qw(field1 field2 field3)}; or like this: my ($field1,$field2,$field3) = @$my_hash{qw(field1 field2 field3)}; If we

Type of argument to keys on reference must be unblessed hashref or arrayref

三世轮回 提交于 2019-12-18 06:48:24
问题 if((scalar keys ($this->{'libraries'}->{$y}->{'cellHash'})) == 0){ This is the line where I am getting the "Type of argument to keys on reference must be unblessed hashref or arrayref" error. Can you help me fix this? I am not posting the code for obvious reasons. 回答1: The new ability of keys to take a reference is broken by design. Perl's development team couldn't figure out how it should work with some references, so it only works for some references. As such, keys 's ability to accept a

How to get the key associated with a hash reference's key in a hash of hashes?

≯℡__Kan透↙ 提交于 2019-12-11 01:11:49
问题 In an attempt to help me learn Perl, I built the following data structure, where the inner hash (/DriveA/archive, etc.) is a hash reference: #The contents of the %properties hash of hashes #The inner hash is a hash reference to a hash named %attributes $VAR1 = { '/DriveA' => { '/DriveA/archive/' => { 'MaxSize' => '20GB', 'Size' => '19GB', 'Free' => '5' }, '/DriveA/current/' => { 'MaxSize' => '20GB', 'Size' => '12GB', 'Free' => '40' } }, '/DriveB' => { '/DriveB/archive/' => { 'MaxSize' => '8GB

Type of argument to keys on reference must be unblessed hashref or arrayref

♀尐吖头ヾ 提交于 2019-11-29 11:09:00
if((scalar keys ($this->{'libraries'}->{$y}->{'cellHash'})) == 0){ This is the line where I am getting the "Type of argument to keys on reference must be unblessed hashref or arrayref" error. Can you help me fix this? I am not posting the code for obvious reasons. The new ability of keys to take a reference is broken by design. Perl's development team couldn't figure out how it should work with some references, so it only works for some references. As such, keys 's ability to accept a reference is documented to be experimental . Unable to resolve this issue, this "feature" was removed 5.24.