Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
For all my fellow software engineers who have yearned for being able to declare local variables in Qlik script subroutines, I have found a workaround for achieving this: declare them as parameters in the SUB's parameter list; like this:
/* SUB printTriangularNumber (pN) - print the (pN)th triangular number
Outputs pN x (pN+1) / 2.
@param pN
*/
SUB printTriangularNumber (pN, /* local variables: */ lTriangularNumber )
LET lTriangularNumber=(pN+1)*pN/2;
TRACE $(lTriangularNumber);
END SUB
CALL printTriangularNumber( (5) );
Notes:
Message was edited by: Angus Monro - * added Exception dot point re this technique not working when the subroutine's CALL passes no parameters; * altered the recommended style - previous I'd demarcated the local variables in the SUB signature with an inline comment both before and after the list of local variables; but having a comment at the end of the list is redundant, since all variables through to the end of the signature will be 'local' when this technique is used. * added a couple of self-indulgent rants about Qlik's language design. It's an ongoing issue and I wish they'd take a community-consultative approach so that impacts of design choices can be assessed. Argh! There I go again!
SUB printTriangularNumber(pN,lTriangularNumber)
LET lTriangularNumber = (pN+1)*(pN/2);
TRACE $(lTriangularNumber);
ENDSUB
CALL printTriangularNumber((5));
TRACE Accessing out of >End sub< $(lTriangularNumber);
I am able to accessing SUB local variable as globally...As you said above the local variable will be automatically deleted on return from the subroutine....