http://qs321.pair.com?node_id=213188

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

I'm working on the regular Perl Quiz of the Week, and while I've got it solved, I don't understand why I had to do something to dwiw. The included code shows the pertinant problem.

I take a line of the box to be put into the output. $putNum is a boolean variable. The problem is that instead of updating $boxLine with the current word number, it continues to put in the number 1.

The problem went away if I instead use a seperate variable in the foreach line and then later assign its value to $boxLine.

Please help me understand why this is happening.

my $wordNum = 0; foreach my $boxLine ( @empty_square) { if ($putNum) { $wordNum ++; my $space = $boxLine =~ s/\./\./g; my $string = sprintf "%-*d", $space, $wordNum; $boxLine =~ s/\.+/$string/o; } else { $boxLine =~ s/\./ /g; } }