in reply to Problem with while loop inside a foreach loop

Simple... don't use $_.

Instead: foreach my $current_building (@buildings) { ...

Within the body of the loop, $current_building will be defined and will contain the current index of the loop.
  • Comment on Re: Problem with while loop inside a foreach loop

Replies are listed 'Best First'.
Re^2: Problem with while loop inside a foreach loop
by Lotus1 (Vicar) on May 03, 2013 at 17:58 UTC

    Anonymous Monk wrote:

    Within the body of the loop, $current_building will be defined and will contain the current index of the loop.

    Actually it will contain the array element not the index of the loop or index of the array. I assume that is what you meant.

      Actually it will contain the array element ...

      Actually, it will contain an alias to the array element. See choroba's reply.

      >perl -wMstrict -le "my @ra = (1, 2, 3, 4); print qq{@ra}; ;; for my $n (@ra) { $n *= $n; } print qq{@ra}; " 1 2 3 4 1 4 9 16