Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
I have a list variable in a Qlik Sense script that is used to configure a set of values the script will iterate over. I implemented the script like this:
SET vList = 'Value1', 'Value2';
FOR EACH element IN $(vList)
TRACE Element is: $(element);
NEXT element;
This works fine and the trace method is called twice, once for each value in the list. However, I have not found a way to declare vList so that it holds a list of just one value. If I set vList like this:
SET vList = 'Value1';
Then all I get is an empty string. How would I define such a single value list? And related, how would I define an empty list?
Come to think of it - you can use square brackets as alternative quotes in a SET statement so this looks much better and more comprehensible:
SET LIST1=['Hello','World'];
SET LIST2=['Hello'];
SET LIST3=[];
FOR i=1 TO 3
FOR EACH var IN $(LIST$(i))
TRACE $(var);
NEXT
NEXT
what's your goal?
The single quotes are stripped by the expansion when you have a single value like that. Use triple quotes (three single quotes) to ensure that the quotes are part of the string and not just delimiters.
SET vList = '''Value1''';
- // use inline instead of variable
- tst_tab:
- load * inline [
- VAL
- Value1
- ];
- FOR EACH element IN FieldValueList('VAL')
- TRACE Element is: $(element) is been added to the field LIST;
- TABLE_NAME:
- load * inline [
- LIST
- $(element)
- ];
- TRACE value added;
- TRACE looping over to next value;
- NEXT element;
- TRACE droping tst_tab table;
- drop Table tst_tab;
Hope this helps Øystein Kolsrud.
hi jontydkpi,
Jonathan Dienst <span class="icon-status-icon icon-mvp" title="Mvp"></span> <span class="icon-status-icon icon-partner" title="Partner"></span> wrote:
The single quotes are stripped by the expansion when you have a single value like that. Use triple quotes (three single quotes) to ensure that the quotes are part of the string and not just delimiters.
SET vList = '''Value1''';
SET vList ='''Value1''';
let vList2 = Len(vList);
FOR EACH element IN $(vList)
TRACE Element is: $(element);
TABLE_NAME:
load * inline [
LIST
$(element)
];
NEXT element;
Exit Script;
it worked
If you prefer to stick to the explicit list here are the variants - so you can test for yourself:
q = Chr(39);
LIST1 = Replace('|Hello|,|World|','|',q);
LIST2 = Replace('|Hello|','|',q);
LIST3 = '';
FOR i=1 TO 3
FOR EACH var IN LIST$(i)
TRACE $(var);
NEXT
NEXT
Come to think of it - you can use square brackets as alternative quotes in a SET statement so this looks much better and more comprehensible:
SET LIST1=['Hello','World'];
SET LIST2=['Hello'];
SET LIST3=[];
FOR i=1 TO 3
FOR EACH var IN $(LIST$(i))
TRACE $(var);
NEXT
NEXT
Nice! Just what I was looking for. Thanks!