Perl: why does $ hashsize = keys $ hash {$ foo} give an experimental warning and how can I write it better?

I have a hash defined by two factors (I don’t know why this is the right term), which I generate as follows:

if (exists $cuthash{$chr}{$bin}){
    $cuthash{$chr}{$bin} += 1;
}else{
    $cuthash{$chr}{$bin} = 1;
}

Later I want to capture the size of each $ chr-part of the hash that works when I do:

for my $chr (sort keys %cuthash){
    my $hashsize = keys $cuthash{$chr};
    ...
}

but I get a warning:

keys on reference is experimental at ../test.pl line 115.

It works, but obviously it is not perfect. What is the best method?

thank

+4
source share
2 answers

If you dereferenced hashref

my $hashsize = keys %{ $cuthash{$chr} };

then there should be no warning.

+7
source

$cuthash{$chr}{$bin}, ++ += 1, , undef.

++$cuthash{$chr}{$bin};

if.

-, each. , , %$hashref %{ $cuthash{$chr} }.

for my $chr (sort keys %cuthash) {
    my $val = $cuthash{$chr};
    my $hashsize = keys %$val;
    ...
}

while ( my ($key, $val) = each %cuthash) {
    my $hashsize = keys %$val;
    ...
}

, $val,

+3

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


All Articles