perl - Different result in forloop? -


i want print this

    xxx    xxxxx   xxxxxxx  xxxxxxxxx xxxxxxxxxxx 

i achive following code

$s = "x"; $z = 5; $m = 1;  $m = $m+2, $z--, $c = " " x $z, $st = $s x $m, print "$c$st\n", for(1..5); 

my doubt

when used increment , decrement operator after print function gave different result script is

$c = " " x $z, $st = $s x $m, print "$c$st\n", $m = $m+2, $z--, for(1..5); 

it result is

     x 35    xxx 54   xxxxx 73  xxxxxxx 92 xxxxxxxxx 

here 3 5 7 9 printed $m , 5 4 3 2 printed $z. but, not directly print $m , $z why gave $m , $z value? how work?

the code

$c = " " x $z, $st = $s x $m, print "$c$st\n", $m = $m+2, $z--, for(1..5); 

is parsed as:

$c = " " x $z, $st = $s x $m, print ("$c$st\n", $m = $m+2, $z--), for(1..5); 

you can force different parsing using parentheses:

$c = " " x $z, $st = $s x $m, print ("$c$st\n"), $m = $m+2, $z-- for(1..5); 

but rather suggest following:

for(1..5) {   $c = " " x $z;   $st = $s x $m;   print ("$c$st\n");   $m = $m+2;   $z--; } 

that way not relying on operator precedence might bite you. see statements contained in loop too. (i had read initial code thrice it)


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -