14 Replies Latest reply: Feb 24, 2014 6:37 AM by Josetxo Josetxo Amonarriz

# 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!

• ###### 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)

Hi Erica,

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.

• ###### 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

• ###### 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)

Thanks a lot, it worked!

• ###### 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]

• ###### 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!

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

T:

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;

• ###### 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!

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

try this:

Values:

Values

1

2

5

7

11

12

];

load concat (distinct Values,',') as Valuelist resident Values;

drop table Values;

let values=peek('Valuelist',0,'Values');

for each v in \$(values)

trace \$(v);

next;

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

with variable values, you can read it from a table (in my example List)

and then check if  they are also in Data table

List:

F3

1

2

5

];

Data:

F1

1

2

3

4

5

6

7

8

9

10

];

LET vList = Peek('F3', 0, 'List');

trace 'vList='  \$(vList);

for each v in \$(vList)

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

if (\$(vF1)=\$(v)) then

trace 'found match v='  \$(v);

end if;

next v;

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

Hi Massimo,

the suggestion from Erica did the job - many thanks for your help.

Cheers.

• ###### 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

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

Hi Alessandro,

the suggestion from Erica did the job - many thanks for your help.

Cheers.