Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Array reference error

by sunil@perl (Initiate)
on Feb 27, 2014 at 13:39 UTC ( #1076394=perlquestion: print w/ replies, xml ) Need Help??
sunil@perl has asked for the wisdom of the Perl Monks concerning the following question:

Hai Monks, I am a perl beginner and trying to experiment on arrays. I came across this error in one of my experiment, Can't use an undefined value as an ARRAY reference at p42svn.pl line 844. The code is as shown below,

@{$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;
Thanks in advance

Comment on Array reference error
Download Code
Re: Array reference error
by toolic (Chancellor) on Feb 27, 2014 at 13:48 UTC
Re: Array reference error
by tobyink (Abbot) on Feb 27, 2014 at 14:12 UTC

    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.

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

      There doesn't seem to be an  'undefined' warning (see perllexwarn), but turning off strict references together with  'uninitialized' warnings (both of these actions taken within the narrowest possible scope, of course) seems to do the trick.

      c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my %res; ;; no strict qw(refs); no warnings qw(uninitialized); ;; @{$res{'act'}} = sort { return 0 unless $a->{'act'} =~ /^move/ and $b->{'act'} =~ /^move/; return $a->{'act'} cmp $b->{'act'}; } @{$res{'acts'}}; dd \%res; " { act => [] }

        Oops, yes. I meant uninitialized. Fixed.

        Turning off strict refs should not be necessary for this.

        use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1076394]
Approved by toolic
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2014-08-22 23:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (168 votes), past polls