Page 1 of 1
How to determine a letter in a variable ?
Posted: Wed Apr 16, 2014 9:06 am
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

Re: How to determine a letter in a variable ?
Posted: Wed Apr 16, 2014 9:18 am
by BariAbdul
Re: How to determine a letter in a variable ?
Posted: Wed Apr 16, 2014 9:49 am
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;
Re: How to determine a letter in a variable ?
Posted: Wed Apr 16, 2014 11:27 am
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
Re: How to determine a letter in a variable ?
Posted: Wed Apr 16, 2014 11:46 am
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?
Re: How to determine a letter in a variable ?
Posted: Wed Apr 16, 2014 12:06 pm
by beber005
Okay with this explication I understand the code. Thank's for your explication Alan

Re: How to determine a letter in a variable ?
Posted: Wed Apr 16, 2014 2:28 pm
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.
Re: How to determine a letter in a variable ?
Posted: Wed Apr 30, 2014 7:28 am
by beber005
Thanks pandinus for your answer, and sorry for my late I didn't see your post
