Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
erikkorme
Contributor III
Contributor III

Start and end of for-loop: Zero-indexed?

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!

1 Solution

Accepted Solutions
Anonymous
Not applicable

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.

View solution in original post

4 Replies
alexandros17
Partner - Champion III
Partner - Champion III

Peek starts from 0 so write:

FOR i=1 to $(NumRows)
     
LET lols = Peek('F1',$(i)-1,'TEST');
     TRACE $(lols);
NEXT

Let me know

Anonymous
Not applicable

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.

fernando_tonial
Partner - Specialist
Partner - Specialist

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

Don't Worry, be Qlik.
erikkorme
Contributor III
Contributor III
Author

Thanks for all your quick replies!

The detail about Peek from the reference manual that Bill is mentioning was what I had missed.

Thanks!