Skip to main content
Announcements
Have questions about Qlik Connect? Join us live on April 10th, at 11 AM ET: SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
salto
Specialist II
Specialist II

Loop with a list of values (not from i to j)

Hello,

I need to loop over the records of a table. Seems to be quite easy: for VAR=0 to NoOfRows('TableName') -1

But this is a problem for me. I need VARto be:

VAR
1
2
5
7
11
12

Otherwise I get an error. Is it  possible to make a loop with a list of values like in the table above? Thanks!

1 Solution

Accepted Solutions
Not applicable

You can loop through a list of values in a table, using the peek function as mentioned above, or fieldvalue (if the values are all distinct

for i = 1 to noofrows('tablename)

let vVar = fieldvalue('fieldname',$(i))

[..caluclations using vVar..]

next i

Erica

View solution in original post

14 Replies
Not applicable

Hi Salto

You could put an if statement inside the do loop:

for var = 0 to noofrows('tablename') -1

if match(var,1,2,5,7,11,12) then

          [.....]

end if

next

Unless you need the list of numbers to be dynamic?

Erica

alexandros17
Partner - Champion III
Partner - Champion III

Use this form of FOr:

for each var in list

[statements]

[exit for [ ( when | unless ) condition ]

[statements]

next[var]

maxgro
MVP
MVP

T:

LOAD * INLINE [

    F1

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

];

for each v in 1, 2, 5                    

  vF1 = Peek('F1', v-1, 'T');

  trace 'v=' & $(v);

  trace 'value=' & $(vF1);

next v;

salto
Specialist II
Specialist II
Author

Hi Erica,

     thanks for your quick answer.

     Yes, as you suggest the list is dynamic: I did only wrote 6 values but the real scenario is a table from which I need to read the VAR values:

for var = 0 to noofrows('tablename') -1

// if match(var,1,2,5,7,11,12) then

if VAR exists in the VAR field then

          [.....]

end if

next

I will try figure out the way to code the if VAR exists in the VAR field then piece of code!

Thanks.

salto
Specialist II
Specialist II
Author

Thanks a lot Alessandro,

     but I am afraid that still I have the same problem as stated on my previous message to Erica: the list of values comes from the values in a table and not from a "closed" list.

     Could I set those values in a list? Thanks!

salto
Specialist II
Specialist II
Author

Hi Massimo,

     thanks, but I honestly do not understand your answer. 1,2 and 5 are the values in my list, but, can I read them from a table? Thanks!

Not applicable

You can loop through a list of values in a table, using the peek function as mentioned above, or fieldvalue (if the values are all distinct

for i = 1 to noofrows('tablename)

let vVar = fieldvalue('fieldname',$(i))

[..caluclations using vVar..]

next i

Erica

Not applicable

also, to check for existence you could use len(value) or value > 0 tocheck for existence of vVar

ERica

alexandros17
Partner - Champion III
Partner - Champion III

Hi,

try this:

for VAR=0 to NoOfRows('myTab') -1

  LET myFldVar = Peek('myFld', $(VAR), 'myTab');

  myFldVar = myFldVar + 1;

NEXT

Where myTab is the table in which you store values and myFld is the field of that table;

The var you have to use is my FldVar