Qlik Community

QlikView Scripting

Discussion Board for collaboration on QlikView Scripting.

qv_mypath
New Contributor

How to assign an output of a user defined function

Hi All,

I would like to assign an output of a function to a variable.
Say a function is just

function test
    test = 3 + 3
end function

While trying to assign to a variable, say, vTestVar, I get an error message:
Error: Error in expression: TEST ist not a valid function.

What am I doing wrong?
Thank you very much in advance.

1 Solution

Accepted Solutions

Re: How to assign an output of a user defined function

You need to add () after the function name

function test ()

    test = 3 + 3

end function



And set the variable in your load script thus, not forgetting the (), I also put a trace after it to display what is in the variable.


let vTestVar = test() ;

trace vTestVar: $(vTestVar) ;

Sample qvw is attached.

8 Replies

Re: How to assign an output of a user defined function

Where you use this function on the load script or some where else.

Regards

Anand

qv_mypath
New Contributor

Re: How to assign an output of a user defined function

Thank you for your response, Anand!

I define the function in the module and then try to assign its output to a variable either in UI or in the load script. Neither works.

Re: How to assign an output of a user defined function

For more details for this please check the Qlik API Guide.

Regards

Anand

Re: How to assign an output of a user defined function

Personally I avoid macros, the reasons are explained in this Blog Post Macros are Bad

Notwithstanding that, what are you trying to achieve with this macro ?

It may be achievable without resorting to a macro.

Re: How to assign an output of a user defined function

Thanks Bill,

I am also agree with you and this macros are time consuming also.

@qv_mypath You can do this with normal tables also with the inline data i represent a small demo to you to explain how to do this calculation with the tables.

//Calculation Done on the Variable

Let vTest = 3 + 3;

//Variable calculation taken on the table

Load * Inline

[

Addition_Result

'$(vTest)'

];

Variables.PNG

You can do any calculation on the script part.

Regards,

Anand

qv_mypath
New Contributor

Re: How to assign an output of a user defined function

Thank you for your response, Bill!

I asked this more to learn about the general possibility (to define any function in macro module and to assign its output to any variable - preferably in UI but at least in the load script). The certain goal can surely be achieved without macro :-)

That said, I am agree that the macros are somewhat less than perfect. But sometimes you need a user-defined function. The best example I can think of at the moment is borrowing functions for regex processing from vbs since you do not have them in the load script out of the box.

Re: How to assign an output of a user defined function

You need to add () after the function name

function test ()

    test = 3 + 3

end function



And set the variable in your load script thus, not forgetting the (), I also put a trace after it to display what is in the variable.


let vTestVar = test() ;

trace vTestVar: $(vTestVar) ;

Sample qvw is attached.

qv_mypath
New Contributor

Re: How to assign an output of a user defined function

Thank you very much! This did the trick for the sample function.

Community Browser