Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
I have a loop in my script, something like below:
For i = 0 TO 10
LET expr = i * 2;
NEXT i
I would like to know whether expr variable created within loop is available outside loop for, for example, could I do something like below?
or I need to declare variable expr before loop for using SET sentence?
For i = 0 TO 10
LET expr = i * 2;
NEXT i
LET another_expr = expr; // What is the value for expr here? 20?
Hi Tony TP,
variable expr is not private to the for loop.
That's why, you can perform transferring value of expr to another_expr.
which is 20 at the end of For Next.
Its interesting to run the loop in debug to follow the iterations.
regards,
jp
Might be from 0 to 9 The values are not correct. That's why the loop working on 10th Value
10 * 2 = 20
Hi Tony TP,
variable expr is not private to the for loop.
That's why, you can perform transferring value of expr to another_expr.
which is 20 at the end of For Next.
Its interesting to run the loop in debug to follow the iterations.
regards,
jp
Qlikview variables are always global scope.
in your case, last value for expr = 10 * 2 = 20
try this, I added some trace to your "for loop"
For i = 0 TO 10
LET expr = i * 2;
trace i=$(i) expr=$(expr);
NEXT i
LET another_expr = expr;
trace another_expr=$(another_expr);
and you get this result
i=0 expr=0
i=1 expr=2
i=2 expr=4
i=3 expr=6
i=4 expr=8
i=5 expr=10
i=6 expr=12
i=7 expr=14
i=8 expr=16
i=9 expr=18
i=10 expr=20
another_expr=20
I would like to know whether expr variable created within loop is available outside loop for, for example, could I do something like below?
yes
or I need to declare variable expr before loop for using SET sentence?
no
LET another_expr = expr; // What is the value for expr here? 20?
yes
Thanks for your example and for your detailed explanation