Skip to main content
Announcements
See what Drew Clarke has to say about the Qlik Talend Cloud launch! READ THE BLOG
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Let scope

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?

1 Solution

Accepted Solutions
jpenuliar
Partner - Specialist III
Partner - Specialist III

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

View solution in original post

5 Replies
Anil_Babu_Samineni

Might be from 0 to 9 The values are not correct. That's why the loop working on 10th Value

10 * 2 = 20

Best Anil, When applicable please mark the correct/appropriate replies as "solution" (you can mark up to 3 "solutions". Please LIKE threads if the provided solution is helpful
jpenuliar
Partner - Specialist III
Partner - Specialist III

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

Clever_Anjos
Employee
Employee

Qlikview variables are always global scope.

in your case, last value for expr = 10 * 2 = 20

maxgro
MVP
MVP

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

Not applicable
Author

Thanks for your example and for your detailed explanation