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

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
_AnonymousUser
Specialist III
Specialist III

Get substr of a global variable

Hello,
On the expression field of a tMap I use the following :

(String)globalMap.get("tFileList_1_CURRENT_FILE")


This variable can have for instance the followings values :
SERVER11_20160101.log
SMTPSERVER11_20160101.log
I would like to get the value betwwen _ and . i.e. 20160101
Could you please tell me how I can do that in the simplest way ?
Thanks in advance
Regards
Frey1

Labels (2)
5 Replies
Anonymous
Not applicable

[font=consolas, monaco, bitstream vera sans mono, courier new, courier, monospace] This should work......[/font] 

((String)globalMap.get("tFileList_1_CURRENT_FILE")).substring(((String)globalMap.get("tFileList_1_CURRENT_FILE")).indexOf("_")+1, (((String)globalMap.get("tFileList_1_CURRENT_FILE")).indexOf(".log")));



It's not simple though. To simplify it, think of it in this way.....


myValue.substring(myValue.indexOf("_")+1, myValue.indexOf(".log"));



You could always create a Routine to do this which will make it even easier to understand.
Anonymous
Not applicable

I should have added, you will want to check for null first and if you cannot ensure that "_" and ".log" will exist, you will need to trap those scenarios as well. I'd create a Routine. But the code I have given you should be a good basis for that.
_AnonymousUser
Specialist III
Specialist III
Author

Thanks for your reply. It works very well 0683p000009MACn.png
When you talk of a routine, do you mean a routine inside the tMap of a new component allowing to make routine ?
Anonymous
Not applicable

If you go to your project tree, look for "Code" and expand it, you will see where the routines are. Routines are essentially Java classes. You can create your own bespoke functions here that can be called anywhere in a Talend job. This is really useful for stuff like this and allows you present the code in a far easier way to read. It also means that you can write functions that require several different lines of code to achieve. Using Java code in tMap components kind of limits you to a single line of code.
_AnonymousUser
Specialist III
Specialist III
Author

Ok, thanks a lot for your help 0683p000009MACn.png