Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: undefined value in array reference

by kennethk (Abbot)
on Mar 31, 2011 at 18:02 UTC ( #896679=note: print w/replies, xml ) Need Help??


in reply to undefined value in array reference

The most likely situation is that you are trying to dereference a row before it is initialized: demonstrated by the code:

#!/usr/bin/perl use strict; use warnings; my @row = ( [1] ) x 3; print @{$row[2]}, "\n"; print @{$row[3]}, "\n";
Are you sure @aob contains what you think it does? See How can I visualize my complex data structure?. You can also wrap your dereference in an eval to catch the case where it fails, like:

sub popnum3 { use Data::Dumper; my ( $x, $y, $z ) = @_; my @row = eval{@{$aob[ $y - 1 ]}} or die "$x\n$y\n$z\n", Dumper $ +aob[ $y - 1 ]; my $mean = mean(@row); $aob[$x][$y] = $mean * ( 1 + $z ); return $aob[$x][$y]; }

Replies are listed 'Best First'.
Re^2: undefined value in array reference
by wind (Priest) on Mar 31, 2011 at 18:15 UTC

    Maybe add some parameter validation to your subroutine to catch out of bound errors?

    sub popnum3 { my ( $x, $y, $z ) = @_; warn "y=$y is out of bounds" if $y - 1 > $#aob || ! ref $aob[ $y - + 1 ]; my @row = @{$aob[ $y - 1 ]}; my $mean = mean(@row); $aob[$x][$y] = $mean * ( 1 + $z ); return $aob[$x][$y]; }

    Update: Meant to be reply to OP

Re^2: undefined value in array reference
by Dandello (Monk) on Mar 31, 2011 at 18:55 UTC

    Okay, using KennethK's snippet above gives : uncaught exception from user code; snip ('4\x{a}501\x{a}\0\x{a}', '$VAR1 = undef;\x{a}')

    Now the row I'm trying to get has already been printed into the data file. But running through every row in the file seems a little memory consuming when the same data should be in the array.

    What seems to be happening is is  @{$aob[ $y - 1 ]} just isn't getting me the row from the array.

    In a different subroutine I have $aob[$x][ $y - 1 ] and there are always values there and it works properly - it's getting that entire row ($y - 1) into a separate array so I can get the mean of the row that's giving me problems.

    The code for getting the row comes from Dennis's Quick Guide and I haven't found that particular algorithm anywhere else.

    It would be nice to get the row algorithm to work as a one-liner. But it would be even nicer to just get it to work.

      At the time of the dereference, the $y-th value of @aob is empty. It's certainly possible for the $y-th value to be empty when the $x-th value is not - $x just has to be less than $y. Perhaps you really mean my @row  = @{$aob[ $x ]};? This sounds very much like an indexing error. A readthrough of perlreftut/perldsc might be helpful.

      Otherwise, I'll need to see your issue in broader context - a complete script with sample input and intended/desired output, all wrapped in <code> tags would be ideal.

        Well, in this particular set of data, $x maxes out at 500 and $y maxes out at 10,000. So yes, it's an indexing issue related to @{$aob[ $y - 1 ]} but nothing I've read points to a simple one-line solution that actually produces the proper output.

        As is my usual, my current solution is a little sideways. In the section above calling popnum3:

        foreach my $r (0 .. $total){ $row[$r] = $aob[$r][$y - 1]; } my @rowin = grep { $_ ne q{} } @row; $mean = mean(@rowin);
        then
        sub popnum3 { my ( $x, $y, $z, $mean ) = @_; $aob[$x][$y] = $mean * ( 1 + $z ); return $aob[$x][$y]; }

        Supposedly @row = @{$aob[ $y - 1 ]} should be equivalent to

        foreach my $r (0 .. $total){ $row[$r] = $aob[$r][$y - 1]; }
        But it didn't work out that way.

        BTW, the grep is in there since I know that although the current parameters shouldn't produce any blanks in the output, other parameters will.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://896679]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2019-10-18 11:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?