in reply to Re^2: Enlarging scalars
in thread Enlarging scalars

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

Replies are listed 'Best First'.
Re^4: Enlarging scalars
by WisDomSeeKer34 (Sexton) on Jun 12, 2017 at 13:44 UTC

    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.

      You'll probably hear this a couple of times .. but telling us that a solution "didn't work" isn't a useful description of what did work.

      Re-declaring new $x and $y variables makes local copies of those variables. Removing the my removes that issue, and should have solved the issue that you were having.

      Show us the new code that "doesn't work" and we'll help you again -- but please, in future :) tell us more than just something "doesn't work".

      Alex / talexb / Toronto

      Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

      Yes - huck solved one of your problems, but there are many problems in your code.

      See {choroba]s advice, or alternatively think about when Perl interpolates $z (and the other variables) into your string.

      Printing your string $a at the various steps might help you understand when interpolation happens.

      Perl interpolates strings at the time of mention, so by first calculating $z and then assigning to $a you should be able to solve the other problem.

      Well of course the values in $a get captured when the assignment is made. What do you think was going to happen?

      for (my $i=1; $i <= $hoeveel; $i++) { $y = $y + 155; $z = $z +1; print <<EOT; <g transform="translate(-124.85653,$y)" id="layer$z"> <text xml:space="preserve" style="font-style:normal;font-variant:normal;font-weight:normal +;font-stretch:normal;font-size:126.0375061px;line-height:125%;font-fa +mily:Arial;-inkscape-font-specification:'Arial, Normal';text-align:st +art;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-ancho +r:start;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stro +ke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" x="-45.714272" y="235.21936" id="text3336"><tspan id="tspan3338" x="-45.714272" y="235.21936">a</tspan></text> </g> EOT };