GeneQ
GeneQ

Reputation: 7595

What must I do to prevent Perl from complaining that "using a hash as a reference is deprecated"?

The code below is from an old Perl script.

print "%{@{$noss}[$i]}->{$sector} \n\n";

How should I rewrite the code above so that Perl does not complain that "using a hash as a reference is deprecated"? I have tried all sorts of way but I still couldn't quite get the hang of what the Perl compiler want me to do.

Upvotes: 3

Views: 338

Answers (3)

ysth
ysth

Reputation: 98398

Guessing that $noss is a reference to an array of hash references, you can build a correct expression by following the simple rule of replacing what would normally be an array or hash name (not including the $/@/%) with an expression giving a reference in curly braces.

So your array element, normally $foo[$i], becomes ${$noss}[$i]. That expression is itself a hashref, so to get an element from that hash, instead of $foo{$sector}, you use ${ ${$noss}[$i] }{$sector}.

This can also appear in various other forms, such as $noss->[$i]{$sector}; see http://perlmonks.org?node=References+quick+reference for simple to understand rules.

Upvotes: 5

Joel Berger
Joel Berger

Reputation: 20280

I agree with ysth and tchrist, and want to reiterate that $noss->[$i]{$sector} really is the best option for you. This syntax is more readable since it shows clearly that $noss is a reference and that you are taking the $ith element of it and further the $sector key from that element.

In terms of teaching to fish rather than giving out fish: you should read perldoc perlreftut and specifically the "use rules". Understanding these two "use rules" along with the extra "arrow rule" (yep only 3 rules) will give you a much better grasp on how to get going with references.

Upvotes: 4

tchrist
tchrist

Reputation: 80405

print "%{@{$noss}[$i]}->{$sector} \n\n";

should be nothing more than

print "$noss->[$i]{$sector} \n\n";

or even

print "$$noss[$i]{$sector} \n\n";

without all that rigamarole.

Upvotes: 11

Related Questions