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

Hi Monks,
I have a a reference to an array of arrays, which I need to loop through to see if
any numbers in the array match the numbers of a for loop. ie:
pseudocode: for( i=1; i<33; i++ ) { foreach ArrayReference in my Reference { foreach value in my ArrayReference { if (value == i ) { cell number must be highlighted. go to next element in FOR loop } } } print regular cell }
I'm building an HTML table with the numbers 1-32, and if any numbers are in any
of these arrays, then the table cell needs to be highlighted.
If this approach is correct, how do I go to the next element in for loop from within a doubly nested foreach?
I am open to suggestions for another approach as well.
Thanks,
Elam

Replies are listed 'Best First'.
Re: Simple looping question.
by Not_a_Number (Prior) on Jan 09, 2004 at 21:18 UTC

    You could adapt the following to suit your purposes:

    my @AoA = ( [ 1, 3, 4, 72, 99 ], [ 1, 4, 32, 38, 92 ], [ 1, 3, 4, 31, 111 ] ); my %to_bold; for ( @AoA ) { $to_bold{$_}++ for @$_; } print $to_bold{$_}? "<b>$_</b> " : "$_ " for 1 .. 32;

    dave

Re: Simple looping question.
by Zed_Lopez (Chaplain) on Jan 09, 2004 at 19:03 UTC
    LABEL: for ... foreach... foreach ... next LABEL;

    but in this specific case, I wouldn't use the outer for loop. Pseudocode below:

    foreach ArrayReference in my Reference { foreach value in my ArrayReference { print (value >=1 and value < 33) ? highlighted cell : regu +lar cell; } }

    I'm assuming value will only be integer. If it could have real values between 1 and 33, my pseudocode would have different behavior than your original pseudocode.

      Thinking about it more, my pseudocode still has different behavior than yours... you'd be starting to print out the entire table as represented by your arrayref of arrayrefs 32 times, starting over every time you hit an integer value between 1 and 32. I'd guess this probably isn't what you want.