Qlik Community

QlikView Scripting

Discussion Board for collaboration on QlikView Scripting.

Announcements

Breathe easy -- you now have more time to plan your next steps with Qlik!
QlikView 11.2 Extended Support is now valid through December 31, 2020. Click here for more information.

salto
Valued Contributor 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!

Tags (3)
1 Solution

Accepted Solutions
Not applicable

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

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

14 Replies
Not applicable

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

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

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

Use this form of FOr:

for each var in list

[statements]

[exit for [ ( when | unless ) condition ]

[statements]

next[var]

MVP
MVP

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

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
Valued Contributor II

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

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
Valued Contributor II

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

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
Valued Contributor II

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

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

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

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

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

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

ERica

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

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