Qlik Community

New to Qlik Sense

Discussion board where members can get started with Qlik Sense.

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?

Tags (1)
1 Solution

Accepted Solutions
MVP
MVP

Re: How do I define list with single value in script?

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



8 Replies
agigliotti
Honored Contributor II

Re: How do I define list with single value in script?

what's your goal?

MVP
MVP

Re: How do I define list with single value in script?

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

ajaykakkar93
Contributor

Re: How do I define list with single value in script?

  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.

Regards & Thanks,
Ajay Kakkar
ajaykakkar93@gmail.com
ajaykakkar93
Contributor

Re: How do I define list with single value in script?

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

Regards & Thanks,
Ajay Kakkar
ajaykakkar93@gmail.com
MVP
MVP

Re: How do I define list with single value in script?

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

MVP
MVP

Re: How do I define list with single value in script?

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
Contributor

Re: How do I define list with single value in script?

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;
Regards & Thanks,
Ajay Kakkar
ajaykakkar93@gmail.com
Employee
Employee

Re: How do I define list with single value in script?

Nice! Just what I was looking for. Thanks!