This probably means that $res{'acts'} is undef, therefore you'll get a warning about trying to treat it like an arrayref. One simple solution is:
@{$res{'act'}} = sort { return 0 unless $a->{'act'} =~ /^move/ and $b->{'act'} =~ /^mo +ve/; return $a->{'act'} cmp $b->{'act'}; # add < delete } @{ $res{'acts'} or [] }; return \%res;
The @{ $res{'acts'} or [] } construct will choose $res{'acts'} (if it evaluates to true) or a reference to an empty array (otherwise), and whichever gets chosen, then dereference the arrayref to get an array.
The other easy way is to simply disable that warning before you attempt the sort...
no warnings qw(uninitialized); @{$res{'act'}} = sort { return 0 unless $a->{'act'} =~ /^move/ and $b->{'act'} =~ /^mo +ve/; return $a->{'act'} cmp $b->{'act'}; # add < delete } @{$res{'acts'}}; return \%res;
Warnings are supposed to be helpful. If you're not finding them helpful, there's no point in suffering them.
In reply to Re: Array reference error
by tobyink
in thread Array reference error
by sunil@perl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |