Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hi,
I'm developing a load script that needs to loop through a table and call a function within the loop. I'm having some trouble with the start and end of the loop.
Can someone confirm if Qlik-scripts are zero indexed or not?
An example:
LOAD * INLINE [
F1
A
B
C
D
E
F
];
LET NumRows = NoOfRows('TEST');
TRACE $(NumRows);
FOR i=1 to $(NumRows)
LET lols = Peek('F1',$(i),'TEST');
TRACE $(lols);
NEXT
I was under the impression that Qlik started lists with 1 and not 0. But that might be wrong? But most examples With usage of the for-loop seems to start with i=1, but that will miss the first element in the table? Am I missing something obvious here?
Thanks for your input!
If you look at the help for peek(), you'll read that 0 denotes the first record. So you need to start your loop at zero.
peek(fieldname [ , row [ , tablename ] ] )
Returns the contents of the fieldname in the record specified by row in the internal table tablename. Data are fetched from the associative QlikView database.
Fieldname must be given as a string (e.g. a quoted literal).
Row must be an integer. 0 denotes the first record, 1 the second and so on. Negative numbers indicate order from the end of the table. -1 denotes the last record read.
If no row is stated, -1 is assumed.
Tablename is a table label, see Table Labels, without the ending colon. If no tablename is stated, the current table is assumed. If used outside the load statement or referring to another table, the tablename must be included.
Examples:
peek( 'Sales' )
returns the value of Sales in the previous record read ( equivalent to previous(Sales) ).
peek( 'Sales', 2 )
returns the value of Sales from the third record read from the current internal table.
peek( 'Sales', -2 )
returns the value of Sales from the second last record read into the current internal table.
peek( 'Sales', 0, 'Tab1' )
returns the value of Sales from the first record read into the input table labeled Tab1.
Load A, B, numsum( B, peek( 'Bsum' ) ) as Bsum...;
creates an accumulation of B in Bsum.
Peek starts from 0 so write:
FOR i=1 to $(NumRows)
LET lols = Peek('F1',$(i)-1,'TEST');
TRACE $(lols);
NEXT
Let me know
If you look at the help for peek(), you'll read that 0 denotes the first record. So you need to start your loop at zero.
peek(fieldname [ , row [ , tablename ] ] )
Returns the contents of the fieldname in the record specified by row in the internal table tablename. Data are fetched from the associative QlikView database.
Fieldname must be given as a string (e.g. a quoted literal).
Row must be an integer. 0 denotes the first record, 1 the second and so on. Negative numbers indicate order from the end of the table. -1 denotes the last record read.
If no row is stated, -1 is assumed.
Tablename is a table label, see Table Labels, without the ending colon. If no tablename is stated, the current table is assumed. If used outside the load statement or referring to another table, the tablename must be included.
Examples:
peek( 'Sales' )
returns the value of Sales in the previous record read ( equivalent to previous(Sales) ).
peek( 'Sales', 2 )
returns the value of Sales from the third record read from the current internal table.
peek( 'Sales', -2 )
returns the value of Sales from the second last record read into the current internal table.
peek( 'Sales', 0, 'Tab1' )
returns the value of Sales from the first record read into the input table labeled Tab1.
Load A, B, numsum( B, peek( 'Bsum' ) ) as Bsum...;
creates an accumulation of B in Bsum.
Peek starts from 0.
Try this:
LET NumRows = NoOfRows('TEST')-1;
TRACE $(NumRows);
FOR i=0 to $(NumRows)
LET lols = Peek('F1',$(i),'TEST');
TRACE $(lols);
NEXT
Thanks for all your quick replies!
The detail about Peek from the reference manual that Bill is mentioning was what I had missed.
Thanks!
Hi peers,
I am late to this post, but I think it helps understand one piece I am not getting.
I have 9 rows in Tab1.
My loop counter is FOR i=1 TO NoOfRows(Tab1).
How come var $(i) is reading 10 rows? (I am calling it in a Text Object in the UI)
Does this mean that the var is counting from 0 even though I specified 1.
Thank you.