in reply to While issues

The code in your update is functionally different than your original code. Did you mean to go from finding one non-empty element to two? (original: $cnr < 1 vs new: $cnr <= 1).

Also, it loops more than necessary. The following fixes this and simplifies the code:

my $cnr = 2; for my $xda ( shuffle 0 .. $total ) { if ( defined $aob[$xda][ $y - 1 ] ) { $aob[$x][$y] = $z + $aob[$xda][ $y - 1 ]; last if --$cnr == 0; } }

Or if you mean to only find one non-empty element:

for my $xda ( shuffle 0 .. $total ) { if ( defined $aob[$xda][ $y - 1 ] ) { $aob[$x][$y] = $z + $aob[$xda][ $y - 1 ]; last; } }

Replies are listed 'Best First'.
Re^2: While issues
by Dandello (Monk) on Feb 03, 2011 at 16:36 UTC

    Your initial solution was SO much better than mine. So that's the one I'm using.

    I needed to find the first random non-blank element. Using shuffle worked, but I was looking for a neater and simpler way.

    Again, thank you.

    My final -

    while (1) { my $xda = int rand $total + 1; if ( defined $aob[$xda][ $y - 1 ] ) { $aob[$x][$y] = $z + $aob[$xda][ $y - 1 ]; last; } }
    . Using 'defined' is actually better in this case than using "" or q{}. No warnings.