Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
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!
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
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
Use this form of FOr:
for each var in list
[statements]
[exit for [ ( when | unless ) condition ]
[statements]
next[var]
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;
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.
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!
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!
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
also, to check for existence you could use len(value) or value > 0 tocheck for existence of vVar
ERica
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