in reply to Variable in a foreach loop

Can you show your complete code that your are attempting to run? I see no declaration or initialization for $max_cols or $max_rows. Assuming they exist elsewhere, this code would just print out the numbers 0 to whatever is in $max_rows a number of times equal to $max_cols+1 and then print the value of $max_rows one additional time.

EDIT: Ah, I think I see the behavior you are trying to illustrate. The last print actually doesn't get the last value ($max_rows) of the inner foreach loop despite the omission of my, it gets the original value scoped outside the loop (I added declarations and initializations).

use strict; use warnings; my $max_cols = 3; my $max_rows = 3; my $row_element = 0; my $col_element = 0; foreach my $col_element (0 .. $max_cols) { foreach $row_element (0 .. $max_rows) { print $row_element; } } print $row_element;

This isn't what I would have expected (obviously from my original post) either, but as haukex pointed out it does appear to be the intended behavior. I've never tried using a foreach loop without my. I guess you would be stuck doing something like this if you wanted the original scalar outside the foreach loop to update.

use strict; use warnings; my $max_cols = 3; my $max_rows = 3; my $row_element = 0; my $col_element = 0; foreach my $col_element (0 .. $max_cols) { foreach my $row_element_a (0 .. $max_rows) { $row_element = $row_element_a; print $row_element; } } print $row_element;

I love it when things get difficult; after all, difficult pays the mortgage. - Dr. Keith Whites
I hate it when things get difficult, so I'll just sell my house and rent cheap instead. - perldigious

Replies are listed 'Best First'.
Re^2: Variable in a foreach loop
by haukex (Archbishop) on Jul 14, 2016 at 13:15 UTC

    Hi perldigious,

    ... and then print the value of $max_rows one additional time.

    ravi45722 is correct that the final print prints "0" - see my reply as to why.

    use strict; use warnings; my $max_cols = 2; my $max_rows = 3; my $row_element = 0; foreach my $col_element (0 .. $max_cols) { foreach $row_element (0 .. $max_rows) { print "$row_element\n"; } } print "Last: $row_element\n"; __END__ 0 1 2 3 0 1 2 3 0 1 2 3 Last: 0

    Regards,
    -- Hauke D

      Thanks haukex, I got there eventually, I just had to tweak the code so it would run to see that behavior and understand what ravi45722 was trying to point out. I would have been confused too if I had tried that and not dug through the foreach documentation, I always use my and explicitly update variables at a higher scope with assignments inside the loop body, so I've never seen this behavior before.

      I love it when things get difficult; after all, difficult pays the mortgage. - Dr. Keith Whites
      I hate it when things get difficult, so I'll just sell my house and rent cheap instead. - perldigious