Author |
Message
|
Vitor |
Posted: Thu Jan 13, 2011 9:28 am Post subject: |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
Diwakar wrote: |
So, the above 2 lines of code should do what I need ? |
I'd have done it the other way (include all the acceptable characters) and saved a cast. Easier to read, less chance you're going to accidently omit something (or a new kind of rubbish will one day turn up) and more efficient when you have a large number of fields to clean.
But the principle is sound.
I'd also code it as a user-defined function to save some typing. _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
Diwakar |
Posted: Thu Jan 13, 2011 9:35 am Post subject: |
|
|
Apprentice
Joined: 06 Apr 2005 Posts: 33
|
Thanks Vitor,
But how do I use TRANSLATE with acceptable chars ? TRANSLATE is used to remove unwanted chars as per the syntax.
Also, the list of acceptable characters might be huge.
thanks _________________ Diwakar. |
|
Back to top |
|
 |
Vitor |
Posted: Thu Jan 13, 2011 10:12 am Post subject: |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
Diwakar wrote: |
the list of acceptable characters might be huge |
It's not huge, it's 200-odd possible printable characters and a one-time cost in typing them if you use a function.
Diwakar wrote: |
But how do I use TRANSLATE with acceptable chars ? |
You do something like this:
Code: |
CREATE FUNCTION CleanseString(IN dirtyString CHARACTER) RETURNS CHARACTER
BEGIN
DECLARE cTemp CHARACTER;
SET cTemp = TRANSLATE(dirtyString,'!@#$%*()0987654321AaBb and so forth','');
RETURN TRANSLATE(dirtyString,cTemp,'');
END;
|
Code given is an untested example, supplied as is with no warranty express or implied accepted, etc, etc. _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
mqjeff |
Posted: Thu Jan 13, 2011 10:33 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
So do we think two translates is more performant than one cast and one translate? |
|
Back to top |
|
 |
Vitor |
Posted: Thu Jan 13, 2011 10:37 am Post subject: |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
mqjeff wrote: |
So do we think two translates is more performant than one cast and one translate? |
We think that. We have some experimental evidence to demonstrate that.
We also think that a string of readable characters that is the set of all characters we could ever want is better and easier to maintain than a list of all the hex values we don't want. Especially with a double byte character set.
We also think other people may have a different view and would want to code it the other way. We think that would work too. _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
Diwakar |
Posted: Thu Jan 13, 2011 10:52 am Post subject: |
|
|
Apprentice
Joined: 06 Apr 2005 Posts: 33
|
Vitor,
My question was the same as mqjeff's : is two translates bettwr than a cast and a translate ? That's why I asked how you'd translate it with acceptable characters.
Since you said you have some evidence, I take it as authentic.
Translate would remove characters from inside the string too, not just leading/trailing. So, I wonder what the consequences would be.
thanks _________________ Diwakar. |
|
Back to top |
|
 |
Vitor |
Posted: Thu Jan 13, 2011 11:03 am Post subject: |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
Diwakar wrote: |
Translate would remove characters from inside the string too, not just leading/trailing. So, I wonder what the consequences would be. |
Using spaces in this method, probably fatal.
As I've said, it's an example, coding the hex should work. Though as with spaces, if you have legitimate tab characters (for instance) in the string that's going to be a problem.
If there's a sporting change that this could happen then yes you need to code 30 odd TRIM statements and accept this is a consequence of the awkward situation you find yourself in where the schema doesn't describe the document properly. Even if the schema describes everything as a string of length 255 you can restrict the contents to valid values. Or even use something like <xs:whitespace value="replace"/> so you only have spaces to deal with.
Or just get the upstream application fixed not to put all this rubbish in the tags in the first place. _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
mgk |
Posted: Thu Jan 13, 2011 1:01 pm Post subject: |
|
|
 Padawan
Joined: 31 Jul 2003 Posts: 1642
|
I have checked and the following code will do what you seem to be asking for:
Code: |
DECLARE input1 CHARACTER 'testmgktest';
SET OutputRoot.XMLNSC.Top.Out1 = TRIM( 'ste' FROM input1);
DECLARE whiteSpace CONSTANT CHARACTER CAST( X'090D0A20' AS CHAR CCSID 1208); /* tab, cr, lf, space */
DECLARE input2 CHARACTER 'foo';
SET input2 = whiteSpace || input2 || whiteSpace;
SET OutputRoot.XMLNSC.Top.Out2 = TRIM( whiteSpace FROM input2); |
The above code produces this output:
Code: |
<Top><Out1>mgk</Out1><Out2>foo</Out2></Top> |
This works because the TRIM function accepts a list of characters for the "singleton" argument, that are processed can be given any order (see example 1). I agree that this is not well documented, and so I have added the above as an example to the docs...
I hope this helps,
Kind Regards, _________________ MGK
The postings I make on this site are my own and don't necessarily represent IBM's positions, strategies or opinions.
Last edited by mgk on Thu Jan 13, 2011 1:13 pm; edited 1 time in total |
|
Back to top |
|
 |
fatherjack |
Posted: Thu Jan 13, 2011 1:08 pm Post subject: |
|
|
 Knight
Joined: 14 Apr 2010 Posts: 522 Location: Craggy Island
|
Diwakar wrote: |
I can't change the 'Awakward situation' for now, as I am not allowed to change all the code written already. So, I'll have to live with TRANSLATE I guess.
DECLARE invisibleCharList CHAR CAST(x'0d0a09 .....' AS CHAR ccsid 1208); -- all the invisible chars go in here
Declare outString CHAR TRANSLATE(sourceString, invisibleCharList);
So, the above 2 lines of code should do what I need ?
thanks. |
Except when you get something else that you weren't expecting ! _________________ Never let the facts get in the way of a good theory. |
|
Back to top |
|
 |
Vitor |
Posted: Thu Jan 13, 2011 1:45 pm Post subject: |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
mgk wrote: |
This works because the TRIM function accepts a list of characters for the "singleton" argument, that are processed can be given any order (see example 1). I agree that this is not well documented, and so I have added the above as an example to the docs... |
Oooooooooo....................  _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
Diwakar |
Posted: Thu Jan 13, 2011 5:48 pm Post subject: |
|
|
Apprentice
Joined: 06 Apr 2005 Posts: 33
|
MGK,
Thanks for the post, and that's exactly what I needed.
I tried TRIM the way you suggested, and it worked for me.
TRANSLATE is messy, and may cause problems in my case because I need only the leading/trailing non-printable characters (including space) removed from an XML tag's value.
They are just 32 characters starting with hexadecimal x'01' upto x'20'. So, my nonPrintableCharsList string is x'010203...08090a0b...0e0f101112...1718191a1b...1e1f20'.
My code is like(for the benefit of others who are facing similar issues):
---------------------------------------------------------------
Declare nonPrintableCharsList CHAR CAST(x'010203...08090a0b...0e0f101112...1718191a1b...1e1f20' AS CHAR ccsid 1208); -- 32 hexadecimal values inside the string
SET OutputRoot.XMLNSC.Message.Header.price = TRIM(nonPrintableCharsList FROM InputRoot.XMLNSC.Message.Header.price);
---------------------------------------------------------------
Just curious, any idea how long it takes for the infocenter updates you made to be available to the rest of the world ? Everyone may not search this forum (may not find this thread so easily even if they search) for missing documentation.
Thanks. _________________ Diwakar. |
|
Back to top |
|
 |
smdavies99 |
Posted: Thu Jan 13, 2011 10:28 pm Post subject: |
|
|
 Jedi Council
Joined: 10 Feb 2003 Posts: 6076 Location: Somewhere over the Rainbow this side of Never-never land.
|
Diwakar wrote: |
Just curious, any idea how long it takes for the infocenter updates you made to be available to the rest of the world ? Everyone may not search this forum (may not find this thread so easily even if they search) for missing documentation.
|
I'll leave the IBM guys to answer the time for the on-line Inforcentre but it will be at least the next fix pack for one that is installed locally on your PC's HDD. _________________ WMQ User since 1999
MQSI/WBI/WMB/'Thingy' User since 2002
Linux user since 1995
Every time you reinvent the wheel the more square it gets (anon). If in doubt think and investigate before you ask silly questions. |
|
Back to top |
|
 |
|