Skip to main content
Woohoo! Qlik Community has won “Best in Class Community” in the 2024 Khoros Kudos awards!
Announcements
Nov. 20th, Qlik Insider - Lakehouses: Driving the Future of Data & AI - PICK A SESSION
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Loop from an array

I am trying to build a script with a loop based on a variable array. My variable can be set as: vYears= '2008,2009,2010'. The loop should then read 2008 and do something with it, read 2009 and do something with it etc. The set of variables is defined by a user. So, I need first to read the array, create a new variable with each year and loop.

Thanks!

1 Solution

Accepted Solutions
Not applicable
Author

Hi Patrick, you can also use the for each syntax. To accomplish this, you would have to declare your variable similar to:

set vYears = '2008','2009','2010'

Then use the for each:

for each y in $(vYears)

statements...

next

Regards

View solution in original post

7 Replies
Not applicable
Author

for i=1 to substringcount ( vYears, ',' )

subfield(vYears, ',' ,i)

next

Something like this!!!



Not applicable
Author

Hi Patrick, you can also use the for each syntax. To accomplish this, you would have to declare your variable similar to:

set vYears = '2008','2009','2010'

Then use the for each:

for each y in $(vYears)

statements...

next

Regards

Not applicable
Author

Hi Ivan, this works finr for years, thanks. However, if I have:

set vSomething = 'aaa', 'bbb';

for each s in $(vSomething) I get the value NULL for s.

Any clue?

Not applicable
Author

Hi Patrick, it strongly depends in the context you use the variable 's', two general scenarios come to my mind:

1. If you expanded the variable like this:

$(s) , qlikview would look for a variable named aaa in the first iteration, since it doesnt find such variable it returns null.

2. if you can to use the s variable as an string, you have to encapsulate the expansion into single quotes, like this:

'$(s)', this way the value would be render as an string.

Hope this helps you.

Regards

Not applicable
Author

My code was missing the single quote as you explained. Thanks!

Not applicable
Author

I have to add something to the discussion:

If the variable vStuff has 2+ string parameter. The iteration using a for...next works fine. However, if there is only one parameter, it returns NULL.

Assume the following code:

for each s in $(vStuff)

something happens

next

If vStuff = '2010'; - works fine

If vStuff = '2009', '2010'; - works fine

If vStuff = 'aaa','bbb'; - works fine

If vStuff = 'aaa'; Doesn't work. s is set to NULL.

Is this supposed to be a normal behaviour?

Not applicable
Author

Interesting, it never ocurred to me before. What I see, is that when you expand a single token string, QlikView again thinks is the name on a variable, to solve this problem, you can declare your variable as follows:

set vStuff="'aaa','bbb'";

set vStuff="'aaa'";

Notice the declaration is the same as before, but with an addition of double quotes encapsulating the tokens, this way you should not have any problem, no matter the quantity of tokens.

Regards