Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
cpalbrecht
Creator
Creator

Dynamic variable in String

Hi,

I have a variable in a String. And I want the String to change when the variable changes.

Let v1 = 1;

Let v2 = 'v1 is $(v1)';

$(v2) --> "v1 is 1"

When I change v1, I want also v2 to be changed:

Let v1 = 2;

$(v2) --> "v1 is 2"

But it doesn't work. It works only the first time I use $(v2). Do you know how to do that?

BR

19 Replies
t_chetirbok
Creator III
Creator III

he doesn't use text object. he needs the variables only in script

marcus_sommer

If I look again on this I think it couldn't work in this way. The important point is when is v2 evaluated and this is before v1 is changed to 2 and therefore didn't impact this change the content of v2 anymore.

What is the aim of your task? Maybe there are other ways to get to your target.

- Marcus

agigliotti
Partner - Champion
Partner - Champion

i'm using Qlik Sense November 2017 SR1 and below the script used:

Let v1 = 3;


Let v2 = '=v1 is ' & $(v1);


trace $(v1);

trace $(v2);


each time you change value of variable v1 also the variable v2 is changed accordingly.

cpalbrecht
Creator
Creator
Author

I wanted to use a SELECT statement in a variable multiple times. And inside the SELECT statement is a variable which is changing. The easiest way is to copy the statement multiple times. But I wanted to avoid this.

cpalbrecht
Creator
Creator
Author

Yes thats right.

But it is not working again if you change v1 again:

Let v1 = 3;

Let v2 = '=v1 is ' & $(v1);

trace $(v1);

trace $(v2);

Let v1 = 4;

trace $(v1);

trace $(v2);

-->

3

=v1 is 3
4
=v1 is 3

agigliotti
Partner - Champion
Partner - Champion

Yes but it works as expected in script.

It's like a programming language you have to assign the new value of v1 to v2 as below:

Let v1 = 3;

Let v2 = '=v1 is ' & $(v1);

trace $(v1);

trace $(v2);

Let v1 = 4;

Let v2 = '=v1 is ' & $(v1);

trace $(v1);

trace $(v2);

marcus_sommer

I think you could use parametrized variables for it - maybe something like this:

let v1 = 'select * from SOURCE where Date >= $1;';

let v2 = '$(v1(''20.12.2017''))';

trace $(v1);

trace $(v2);

- Marcus

cpalbrecht
Creator
Creator
Author

Oh, yes that is possible.

I tried it. I have two variables. So I can do:

Let v1 = 'this is param1 $1 and this is param2 $2';

Let v2 = '$(v1("1 2 3", "1, 2, 3"))';

Trace $(v2);


But the second varible has a comma inside the string. And the result is then:


--> this is param1 "1 2 3" and this is param2 "1;


How can I avoid that?


agigliotti
Partner - Champion
Partner - Champion

you could use a semicolon rather than comma as below:

Let v1 = 'this is param1 $1 and this is param2 $2';

Let v2 = '$(v1("1 2 3", "1;2;3"))';

and after replace semicolon with comma.

Let v3 = Replace(v2, '|', ',');

Trace $(v3);

marcus_sommer

Unfortunately there is no way to mask a comma within a variable-parameter so that you need workarounds with replace or similar like hinted by Andrea or to avoid them completely. Are there not many possible parameter sub-parts (delimited by the comma) you could just use more parameters. But I assume you want to use field-lists or even expressions and than it would be quite ugly with a variety of parameters.

Please provide some more context to your task because I believe there could be other ways, too. I mean examples of v1 and v2 and how they are created and called - in any loops?

- Marcus