mosh has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I've got this error:

Can't use an undefined value as an ARRAY reference at line ...(the foreach line...)

$self->{entries} = []; push(@{$self->{entries}}, { a => $a, b => $b}); my $entries = ''; foreach (my $i=0; $i<scalar(@{$self->{entries}}); $i++) { ... }
What's the meaning of the mentioned error?
What might be the problem?

Mosh.

Replies are listed 'Best First'.
Re: Can't use an undefined value as an ARRAY...
by jeffa (Bishop) on May 17, 2005 at 17:52 UTC

    I'm with cog on this one. I can't make heads or tails as to what you are trying to do here. Note that use of $a and $b should be limited to sort, but that shouldn't cause the "problem" ... if there even is one. I boiled your code down and it seems ... fine?

    use strict; use warnings; use Data::Dumper; my $entries = []; push(@{$entries}, { a => 1, b => undef}); foreach (my $i=0; $i<scalar(@{$entries}); $i++) { print Dumper $entries->[$i]; }

    Although i would rewrite the foreach loop as such:

    for my $i (0 .. $#$entries) { print Dumper $entries->[$i]; }
    TIMTOWTDI.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Can't use an undefined value as an ARRAY...
by cog (Parson) on May 17, 2005 at 17:48 UTC
    It's hard to say, because the code you provide doesn't produce an error (except for the "..." part, of course).

    Try limiting the code you have to a small case that still produces an error and post it again (perhaps as a reply to this one), so that we can try again.

    Regards,

    jac

Re: Can't use an undefined value as an ARRAY...
by Anonymous Monk on Oct 08, 2014 at 20:23 UTC
    Changing = @{$rowref} ; to = \@{$rowref} ; worked for me ;-)