how to read a string to extract the content?

Post Reply
beber005
Posts: 57
Joined: Mon Mar 03, 2014 2:18 pm
OLAP Product: Cognos
Version: 9.5.1
Excel Version: 2010

how to read a string to extract the content?

Post by beber005 »

Hey every one,

I'm sorry for this question but I did not success to do that. I have a small problem with this sting (for example) : N/ABFG/P/1011
I want to delete the characters located after the last slah.

If someone has a solution thank's a lot for your attention.
Alan Kirk
Site Admin
Posts: 6667
Joined: Sun May 11, 2008 2:30 am
OLAP Product: TM1
Version: PA2.0.9.18 Classic NO PAW!
Excel Version: 2013 and Office 365
Location: Sydney, Australia
Contact:

Re: how to read a string to extract the content?

Post by Alan Kirk »

beber005 wrote:Hey every one,

I'm sorry for this question but I did not success to do that. I have a small problem with this sting (for example) : N/ABFG/P/1011
I want to delete the characters located after the last slah.

If someone has a solution thank's a lot for your attention.
You would need to find the location of the last slash. One way of doing this is to create a loop to call the Scan function until it returns 0. Then you can simply use the last location that you found in conjunction with the SubSt function to parse out the part before the last slash.

Alternatively you could just get the length of the string using the Long function, then keep using the SubSt function to check each character from the last one backwards until you hit a slash. Then it's just a case of making another call to SubSt to get the characters that appear before the slash. I'm sure that a perusal of the Text Rules functions will give you some further ideas.

However the best one of all may be to fix the data at the source so that it's supplying values without those surplus characters in the first place...
"To them, equipment failure is terrifying. To me, it’s 'Tuesday.' "
-----------
Before posting, please check the documentation, the FAQ, the Search function and FOR THE LOVE OF GLUB the Request Guidelines.
beber005
Posts: 57
Joined: Mon Mar 03, 2014 2:18 pm
OLAP Product: Cognos
Version: 9.5.1
Excel Version: 2010

Re: how to read a string to extract the content?

Post by beber005 »

Thank you very much for your response. I have a question because I thought well the SCAN function, but the problem is that I have 3 / my sentence. What do you think of this code? :

Code: Select all

vOTP1 = Scan('/',vOTP);
While (vOTP1 <> 0);
	vNewOTP = Subst(vOTP1,?,?);
beber005
Posts: 57
Joined: Mon Mar 03, 2014 2:18 pm
OLAP Product: Cognos
Version: 9.5.1
Excel Version: 2010

Re: how to read a string to extract the content?

Post by beber005 »

Rather that

Code: Select all

vOTP1 = SCAN('/',vOTP);
While (vOTP1 <> 0);
	voldOTP1 = NumberToString(vOTP1);
	vNewOTP = SUBST('voldOTP1',vOTP1,vOTP1);
End;
What do you think ?
declanr
MVP
Posts: 1831
Joined: Mon Dec 05, 2011 11:51 am
OLAP Product: Cognos TM1
Version: PA2.0 and most of the old ones
Excel Version: All of em
Location: Manchester, United Kingdom
Contact:

Re: how to read a string to extract the content?

Post by declanr »

Code: Select all

iCheck = 0;
iChar = long ( vOPT);
While ( iCheck = 0 );
      sChar = subst ( vOPT, iChar, 1 );
      If ( sChar @='/' );
            iCheck = 1;
            sTrim = Subst ( vOPT, 1, iChar );
      Else;
            iChar = iChar - 1;
      Endif;
End; 
Declan Rodger
beber005
Posts: 57
Joined: Mon Mar 03, 2014 2:18 pm
OLAP Product: Cognos
Version: 9.5.1
Excel Version: 2010

Re: how to read a string to extract the content?

Post by beber005 »

Thank's a lot for your answer !! :)

What do you think about my code ? :

Code: Select all

longueur = LONG(vOTP);
vNewOTP = SUBST(vOTP,1,(longueur-5));
This is the same thing no ?
declanr
MVP
Posts: 1831
Joined: Mon Dec 05, 2011 11:51 am
OLAP Product: Cognos TM1
Version: PA2.0 and most of the old ones
Excel Version: All of em
Location: Manchester, United Kingdom
Contact:

Re: how to read a string to extract the content?

Post by declanr »

beber005 wrote:Thank's a lot for your answer !! :)

What do you think about my code ? :

Code: Select all

longueur = LONG(vOTP);
vNewOTP = SUBST(vOTP,1,(longueur-5));
This is the same thing no ?
I assumed that the challenge was finding out how many characters to remove but yes if you know its always 4 then your subst code will work.

Just testing it in your model and assessing the result will always be your best test though.
Declan Rodger
beber005
Posts: 57
Joined: Mon Mar 03, 2014 2:18 pm
OLAP Product: Cognos
Version: 9.5.1
Excel Version: 2010

Re: how to read a string to extract the content?

Post by beber005 »

After reflexion yes it is always 4. I keep your code and I thank you very much for your help "Declanr".
beber005
Posts: 57
Joined: Mon Mar 03, 2014 2:18 pm
OLAP Product: Cognos
Version: 9.5.1
Excel Version: 2010

Re: how to read a string to extract the content?

Post by beber005 »

I was testing your program and it works but it returns to me, at the end of the expression a slah (/). But I don't want this character and I don't arrive to delete it in your code. I think I have just to put a minus 1 but where ? I don't find the solution. I'm so sorry for this lost of time :oops:
Alan Kirk
Site Admin
Posts: 6667
Joined: Sun May 11, 2008 2:30 am
OLAP Product: TM1
Version: PA2.0.9.18 Classic NO PAW!
Excel Version: 2013 and Office 365
Location: Sydney, Australia
Contact:

Re: how to read a string to extract the content?

Post by Alan Kirk »

beber005 wrote:I was testing your program
Presumably you are referring to Declan's code in the 5th post, as opposed to yours. Which, we have apparently established, is all that you need to do if you do already know how many characters need to be removed in each case. Though again it would (generally) be far more efficient to have them removed from the data source.
beber005 wrote:and it works but it returns to me, at the end of the expression a slah (/).
Yes it does.
beber005 wrote:But I don't want this character and I don't arrive to delete it in your code. I think I have just to put a minus 1 but where ? I don't find the solution.
Seriously? You've read the documentation on SubSt, and you don't get what Declan's code is doing?

Declan's code is counting back from the last character position in the string.

Consequently when sChar hits a \ character, counting down from the full string length, iChar is telling you what the position of that character is in the string.

In your example, with the character numbers shown underneath:

Code: Select all

N/ABFG/P/1011

0000000001111
1234567890123
The last slash is at character number *9*. His code has counted back 13 (the last character in the string, not a slash), 12 (not a slash), 11 (not a slash), 10 (not a slash), 9 (Eureka, it's a slash).

The code

Code: Select all

sTrim = Subst ( vOPT, 1, iChar );
is extracting the first how many characters from the original string?

The first *9* characters, the value of the iChar variable. That will of course include the 9th character which, as we established above, is the slash.

Given all of that, could you perhaps make an educated guess as to where you need to subtract 1 to exclude the slash?
beber005 wrote:I'm so sorry for this lost of time :oops:
The colloquialism that you're looking for is actually "waste of time".
"To them, equipment failure is terrifying. To me, it’s 'Tuesday.' "
-----------
Before posting, please check the documentation, the FAQ, the Search function and FOR THE LOVE OF GLUB the Request Guidelines.
beber005
Posts: 57
Joined: Mon Mar 03, 2014 2:18 pm
OLAP Product: Cognos
Version: 9.5.1
Excel Version: 2010

Re: how to read a string to extract the content?

Post by beber005 »

Sorry for my poor english and for your answer.
Post Reply