Grigor
Grigor

Reputation: 4049

Using hash as a reference is deprecated

I searched SO before asking this question, I am completely new to this and have no idea how to handle these errors. By this I mean Perl language.

When I put this

%name->{@id[$#id]} = $temp;

I get the error Using a hash as a reference is deprecated

I tried

$name{@id[$#id]} = $temp

but couldn't get any results back.

Any suggestions?

Upvotes: 2

Views: 6576

Answers (4)

keegan2149
keegan2149

Reputation: 41

The popular answer is to just not dereference, but that's not correct. In other words %$hash_ref->{$key} and %$hash_ref{$key} are not interchangeable. The former is required to access a hash reference nested as an element in another hash reference.

For many moons it has been common place to nest hash references. In fact there are several modules that parse data and store it in this kind of data structure. Instantly depreciating the behavior without module updates was not a good thing. At times my data is trapped in a nested hash and the only way to get it is to do something like.

$new_hash_ref = $target_hash_ref->{$key1}
$new_hash_ref2 = $target_hash_ref->{$key2}
$new_hash_ref3 = $target_hash_ref->{$key3}

because I can't

foreach my $i(keys(%$target_hash_ref)) {
    foreach(%$target_hash_ref->{$i} {
        #do stuff with $_
    }
}

anymore.

Yes the above is a little strange, but creating new variables just to avoid accessing a data structure in a certain way is worse. Am I missing something?

Upvotes: 0

ikegami
ikegami

Reputation: 385907

%name->{...}

has always been buggy. It doesn't do what it should do. As such, it now warns when you try to use it. The proper way to index a hash is

$name{...}

as you already believe.


Now, you say

$name{@id[$#id]}

doesn't work, but if so, it's because of an error somewhere else in the code. That code most definitely works

>perl -wE"@id = qw( a b c ); %name = ( a=>3, b=>4, c=>5 ); say $name{@id[$#id]};"
Scalar value @id[$#id] better written as $id[$#id] at -e line 1.
5

As the warning says, though, the proper way to index an array isn't

@id[...]

It's actually

$id[...]

Finally, the easiest way to get the last element of an array is to use index -1. The means your code should be

$name{ $id[-1] }

Upvotes: 4

starbolin
starbolin

Reputation: 840

If you want one item from an array or hash use $. For a list of items use @ and % respectively. Your use of @ as a reference returned a list instead of an item which perl may have interpreted as a hash.

This code demonstrates your reference of a hash of arrays.

#!/usr/bin perl -w
my %these = ( 'first'=>101,
             'second'=>102,
           );
my @those = qw( first second );
print $these{$those[$#those]};

prints '102'

Upvotes: -2

Borodin
Borodin

Reputation: 126722

The correct way to access an element of hash %name is $name{'key'}. The syntax %name->{'key'} was valid in Perl v5.6 but has since been deprecated.

Similarly, to access the last element of array @id you should write $id[$#id] or, more simply, $id[-1].

Your second variation should work fine, and your inability to retrieve the value has an unrelated reason.

Write

$name{$id[-1]} = 'test';

and

print $name{$id[-1]};

will display test correctly

Upvotes: 8

Related Questions