Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in Bucharest on Sept 18th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
mikewt
Contributor
Contributor

User-defined Function not working

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?

4 Replies
tresesco
MVP
MVP

Try removing the extra parenthesis before dollar expansion like:

$(ShuffleName([name])) as NewName
mikewt
Contributor
Contributor
Author

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.

tresesco
MVP
MVP

It is actually working with me with your sample data.

marcus_sommer

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