Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
I am trying to come up with a user-defined function inside qlik script with couple of string functions chained together. Looks something like
(Trim(SubField(Subfield([name], ',', -1), '[', 1)) & ' ' & SubField([name], ',', 1)) as NewName
Since this is being used in multiple places I wanted to create a user-defined function to clean it up a bit, to make it look something like
$(ShuffleName([name])) as NewName
I tried creating a function like below
SET ShuffleName = Trim(SubField(Subfield($1, ',', -1), '[', 1)) & ' ' & SubField($1, ',', 1);
But I am having hard time to make it work. Can anyone point out if anything is wrong here?
Try removing the extra parenthesis before dollar expansion like:
$(ShuffleName([name])) as NewName
Thanks for reply.
Are you sure thats the only issue? Becuase I tried to print it out on console with TRACE, like this
SET newname = 'debug: ' & $(ShuffleName('Ipsum, Lorem [1]'));
TRACE $(newname);
But that didn't work either.
It is actually working with me with your sample data.
I could agree your origin approach is working. The question is more how do you exactly defining and calling the function and with which data - all mentioned parts are important to get a working solution. Further if you says it didn't work you should say what happens: nothing, an error (which one) or you get a wrong/unexpected result and in which way is it wrong - means something it returns x whereby you would expect to get y?
Your tried debugging check did not work because you add here
'Ipsum, Lorem [1]'
as parameter which contained a comma which is mandatory treated as parameter delimiter which means it transfers just 'Ipsum which isn't just only a part of your wanted value else it missed a closing single-quote which includes everything following into the logic until the next single-quote appears - this will probably mostly cause an runtime error.
Here what for me worked:
SET ShuffleName = Trim(SubField(Subfield($1, ',', -1), '[', 1)) & ' ' & SubField($1, ',', 1);
load *, $(ShuffleName(name)) as newname;
load * inline "
name
Ipsum, Lorem [1]
" (txt, delimiter is \t);
- Marcus