Skip to main content
Announcements
See what Drew Clarke has to say about the Qlik Talend Cloud launch! READ THE BLOG
cancel
Showing results for 
Search instead for 
Did you mean: 
helen_pip
Creator III
Creator III

Help to understand the For function in Qlikview


Hello

I have plaguerised some code off the internet, which works for what I need.

I load in my expressions table and then the code loops round and stores the variable as an expression in my QVW

I then use the variable name in my charts/tables and apply dollar expansion to call the expression

However I do not understand what the FOR function of the code is doing, and I was wondering if someone could explain why I have the

following syntex in my code

Could someone kidnly explain in Leymans Terms?

For vI = 0 to (vNumberOfRows - 1)

Entire Code below

 

Expressions:

LOAD Variable,
Replace(Expression, '$', '#') As Expression
FROM

(
biff, embedded labels, table is [Formulas$]);



Let vNumberOfRows
=

NoOfRows('Expressions');

For vI = 0 to (vNumberOfRows - 1)

Let vVariable_Name = Peek('Variable',vI,'Expression');

Let [$(vVariable_Name)] = Replace(

Peek('Expression',vI,'Expression'), '#', '$');

Next

Thanks

Helen

2 Replies
Gysbert_Wassenaar

See the help file:

For..next

The for..next control statement is a script iteration construct with a counter. The statements inside the loop enclosed by for and next will be executed for each value of the counter variable between specified low and high limits. The syntax is:

forcounter = expr1 to expr2 [ step expr3 ]

[statements]

[exit for [ ( when | unless ) condition ]

[statements]

next[counter]

Where:

counter is a variable name. If counter is specified after next it must be the same variable name as the one found after the corresponding for.

expr1 is an expression which determines the first value of the counter variable for which the loop should be executed.

expr2 is an expression which determines the last value of the counter variable for which the loop should be executed.

expr3 is an expression which determines the value indicating the increment of the counter variable each time the loop has been executed.

condition is a logical expression evaluating to true or false.

statements is any group of one or more QlikView script statements.

The expressions expr1, expr2 and expr3 are only evaluated the first time the loop is entered. The value of the counter variable may be changed by statements inside the loop, but this is not good programming practice.

If an exit for clause is encountered inside the loop, the execution of the script will be transferred to the first statement after the next clause denoting the end of the loop. An exit for clause can be made conditional by the optional use of a when or unless suffix.

Since the for..next statement is a control statement and as such is ended with either a semicolon or end-of-line, each of its three possible clauses (for..to..step, exit for and next) must not cross a line boundary.

Examples:

// load files file1.csv..file9.csv

FOR a=1 to 9

LOAD * FROM file$(a).csv;

NEXT

FOR counter=1 to 9 step 2

SET filename=x$(counter).csv;

IF rand( )<0.5 THEN

EXIT For Unless counter=1

END IF

LOAD a,b FROM $(filename);

NEXT


talk is cheap, supply exceeds demand
Not applicable

The FOR construction is the actual loop. It will start at 0 (which is the first entry) and loop to the maximum of rows -1 (which is you last entry). For each found record it executes the Peek and Replace functions.

FOR is a common loop in programming languages. It will start at a certain value and then repeat the included statements until a specific value is reached (or a condition is met). The common Syntax is:

For <start> to <max value>

     <conditions>

Next

I hope this helps you.