How to determine a letter in a variable ?

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 determine a letter in a variable ?

Post by beber005 »

Hello everyone,
I would write on the forum because I can not find a viable solution to my problem.
I have a variable of type string "1002" for example. The problem is that this variable can contain letters such as the value "AC31". Or if I have a letter, I want to assign the following value "Aircraft_NA'' to my variable. If instead I do not have a letter I do my usual treatment is a simple concatenation.I initiated a solution which is as follows:

Code: Select all

If(StringToNumber(vMsn) > 0);
	vNewMSN = 'MSNID_' | vMSN;
Else;
	vNewMSN = 'Aircraft_NA';
EndIf;
The problem of course is that when it encounters a value as "AC31" he told me he can not turn it into digital.

So I'm stuck. I have can not be taken the right angle of attack can be you get to tell me what to follow. Thank you in advance for your help :)
BariAbdul
Regular Participant
Posts: 424
Joined: Sat Mar 10, 2012 1:03 pm
OLAP Product: IBM TM1, Planning Analytics, P
Version: PAW 2.0.8
Excel Version: 2019

Re: How to determine a letter in a variable ?

Post by BariAbdul »

Hope this thread might help you:
http://www.tm1forum.com/viewtopic.php?f=3&t=3709
"You Never Fail Until You Stop Trying......"
rmackenzie
MVP
Posts: 733
Joined: Wed May 14, 2008 11:06 pm

Re: How to determine a letter in a variable ?

Post by rmackenzie »

Code: Select all

sVariable = 'abc123xyz';
nVariableHasNonNumerics = 0;

nCharacterCounter = 1;
nLength = LONG ( sVariable );
WHILE ( nCharacterCounter <= nLength );

  nCode = CODE ( sVariable, nCharacterCounter );

  IF ( nCode < 48 % nCode > 57 );
    nVariableHasNonNumerics = 1;
  ENDIF;

  nCharacterCounter = nCharacterCounter + 1;

END;

IF ( nVariableHasNonNumerics = 1 );
  # you found a letter, or other non-numeric character in the string
ENDIF;
Robin Mackenzie
beber005
Posts: 57
Joined: Mon Mar 03, 2014 2:18 pm
OLAP Product: Cognos
Version: 9.5.1
Excel Version: 2010

Re: How to determine a letter in a variable ?

Post by beber005 »

Thanks for your answer but your code does not work for my problem =/
it does not distinguish with a variable with only numbers and a variable having at least one letter
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 determine a letter in a variable ?

Post by Alan Kirk »

beber005 wrote:Thanks for your answer but your code does not work for my problem =/
it does not distinguish with a variable with only numbers and a variable having at least one letter
If you're referring to the post that you were pointed to in the first reply in this thread, no, that isn't relevant to your problem.

If you're referring to Robin's post, try reading what the code actually does.

- It loops through each character of the variable's value.
- It determines the ASCII code of that character. The character "0" has an ASCII code of 48, the character "9" has an ASCII code of 57. The characters for all of the other digits fall in between.
- Consequently if the ASCII code of the character is LOWER than 48 or HIGHER than 57, it must be something that is non-numeric. In such a case the nVariableHasNonNumerics flag is set to 1.

If nVariableHasNonNumerics finishes with a value of 0, then the variable MUST have only numbers because the ASCII codes of each and every character fall somewhere between the codes for "0" and "9". If it finishes with a value of 1 it has AT LEAST one character which is NOT numeric.

Do you see how it works now?
"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 determine a letter in a variable ?

Post by beber005 »

Okay with this explication I understand the code. Thank's for your explication Alan :D
pandinus
Posts: 78
Joined: Tue Mar 18, 2014 8:02 am
OLAP Product: TM1, Cognos Express
Version: 10.2.2
Excel Version: 2013

Re: How to determine a letter in a variable ?

Post by pandinus »

You can optimize this code a bit by:

Code: Select all

WHILE ( nCharacterCounter <= nLength & nVariableHasNonNumerics = 0);

  nCode = CODE ( sVariable, nCharacterCounter );

  IF ( nCode < 48 % nCode > 57 );
    nVariableHasNonNumerics = 1;
  ENDIF;

  nCharacterCounter = nCharacterCounter + 1;

END;
Or even easier:

Code: Select all

WHILE ( nCharacterCounter <= nLength);

  nCode = CODE ( sVariable, nCharacterCounter );

  IF ( nCode < 48 % nCode > 57 );
    nVariableHasNonNumerics = 1;
    nCharacterCounter = nLength + 1;

  ENDIF;

  nCharacterCounter = nCharacterCounter + 1;

END;
In either case the scan does not check any other characters after a non-numeric character has already been found.
Depending on the length of your strings this might save some processing time.
beber005
Posts: 57
Joined: Mon Mar 03, 2014 2:18 pm
OLAP Product: Cognos
Version: 9.5.1
Excel Version: 2010

Re: How to determine a letter in a variable ?

Post by beber005 »

Thanks pandinus for your answer, and sorry for my late I didn't see your post :oops:
Post Reply