in reply to Enlarging scalars

I suspect you want this instead

for (my $i=1; $i <= $hoeveel; $i++) { $y = $y + 155; $z = $z +1; print "$a\n"; };
The extra my commands isolated the variables outside of the loop from any changes but it also meant the inner variables became almost static

Replies are listed 'Best First'.
Re^2: Enlarging scalars
by Anonymous Monk on Jun 12, 2017 at 13:04 UTC

    What's the difference?

      The original code had my $z = $z + 1;, this code has $z = $z + 1;

      Like huck already said, "The extra my commands isolated the variables outside of the loop from any changes but it also meant the inner variables became almost static".

      Using my $z you declare a second variable named $z, which shadows the first variable named $z declared at the top of your script. All statements mentioning $z will refer to that shadowing $z until execution leaves the block for the next loop iteration. In the next iteration, a new shadowing $z is created and all changes made previously have been lost.

      use strict; use warnings; { print "using exterior values\n"; my $y=1; my $z=1; my $hoeveel=5; for (my $i=1; $i <= $hoeveel; $i++) { $y = $y + 155; $z = $z +1; print "$y $z\n"; }; } { print "using localizing interior values\n"; my $y=1; my $z=1; my $hoeveel=5; for (my $i=1; $i <= $hoeveel; $i++) { my $y = $y + 155; my $z = $z +1; print "$y $z\n"; }; }
      Result
      using exterior values 156 2 311 3 466 4 621 5 776 6 using localizing interior values 156 2 156 2 156 2 156 2 156 2

        I did what you did and left out the "my" but it didn't work. I get the same values for $y and $z.

        Look where the $y and $z scalars are located. They are in the $a scalar. The values in the $a scalar needs to be changed.