Skip to main content
Announcements
Introducing Qlik Answers: A plug-and-play, Generative AI powered RAG solution. READ ALL ABOUT IT!
cancel
Showing results for 
Search instead for 
Did you mean: 
datanibbler
Champion
Champion

Move a variable to the script

Hi,

I have tried to move a variable that I have in one app from the GUI to the script.

It is a variable that can only be filled on the GUI as it uses the user's selections. In principle it works - I need double >> = << and a SET for that, I've done it before - but for some reason, there is a set_expression Year={$(=max(Year))} - when I define the variable in the script with the SET statement and double >> == <<, I can see it in the variable_viewer and it looks all right, only that set_expression then reads >> {<Year = {}>}.

I have tried with a variable to simply mirror the selection on the YEAR field and that works fine - it seems to be that $(). I remember that does not work in the script. So I need to find a way around that.

I am experimenting with concatenating that as a string from various elements. I just haven't hit the right one yet.

Can somebody lend me a hand with that, please?

Thanks a lot!

Best regards,

DataNibbler

3 Replies
Peter_Cammaert
Partner - Champion III
Partner - Champion III

The gist of it is that $-sign expansion is performed immediately everywhere it is encountered. In the script, this means even before the statement is parsed and executed, so that things like actually do work

SET SETthatISaLET = LET;

$(SETthatISaLET) var = Today(); // Will contain todays date

But that also means that in order to have delayed (second-level) $-sign expansion, you have to resort to $-sign camouflaging. Which means breaking the structure of a $(...) construct so that the script statement processor won't see it as a $(...) construct, but the end-result will still contain a $(...) construct.

There are multiple ways to get delayed expansion (meaning "not in the script execution, but later on when variables are actually used"), amongst them the Replace trick from the article that Sunny linked to.

IMHO the easiest one is to use explicit string pieces and separate the $ character from the parentheses. Like

LET vDelayed$Sign = '=$' & '(=Today())'; // Concat after preprocessor hasn't found a $-sign exp.

or

LET vDelayed$Sign = chr(36) & '(=Today())'; // Concat after preprocessor hasn't found a $-sign exp.

Best,

Peter

Peter_Cammaert
Partner - Champion III
Partner - Champion III

BTW $-sign expansion does work in a Load Script. But in your situation it works too early.