Skip to main content
Announcements
Live today at 11 AM ET. Get your questions about Qlik Connect answered, or just listen in. SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
Øystein_Kolsrud
Employee
Employee

How do I define list with single value in script?

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?

1 Solution

Accepted Solutions
petter
Partner - Champion III
Partner - Champion III

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



View solution in original post

8 Replies
agigliotti
Partner - Champion
Partner - Champion

what's your goal?

jonathandienst
Partner - Champion III
Partner - Champion III

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''';

Logic will get you from a to b. Imagination will take you everywhere. - A Einstein
ajaykakkar93
Specialist III
Specialist III

  1. // use inline instead of variable
  2. tst_tab:
  3. load * inline [
  4. VAL
  5. Value1
  6. ];
  7. FOR EACH element IN FieldValueList('VAL')
  8.      TRACE Element is: $(element) is been added to the field LIST;
  9.      TABLE_NAME:
  10.      load * inline [
  11.        LIST
  12.        $(element)
  13.      ];
  14.      TRACE value added;
  15.      TRACE looping over to next value;
  16. NEXT element;
  17. TRACE droping tst_tab table;
  18. drop Table tst_tab;

Hope this helps Øystein Kolsrud.

Please mark the correct replies as Solution. Regards, ARK
Profile| GitHub|YouTube|Extension|Mashup|Qlik API|Qlik NPrinting

ajaykakkar93
Specialist III
Specialist III

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

Please mark the correct replies as Solution. Regards, ARK
Profile| GitHub|YouTube|Extension|Mashup|Qlik API|Qlik NPrinting

petter
Partner - Champion III
Partner - Champion III

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

petter
Partner - Champion III
Partner - Champion III

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



ajaykakkar93
Specialist III
Specialist III

yes this also worked,

i re edited it

  1. SET l1=['Hello'];
  2.   FOR EACH out IN $(l1)
  3.     TRACE $(out);
  4. TABLE_NAME:
  5.      load * inline [
  6.        LIST
  7.        $(out)
  8.      ];
  9.   NEXT
  10. Exit Script;

Please mark the correct replies as Solution. Regards, ARK
Profile| GitHub|YouTube|Extension|Mashup|Qlik API|Qlik NPrinting

Øystein_Kolsrud
Employee
Employee
Author

Nice! Just what I was looking for. Thanks!