Testing a string for alpha characters.

Testing a string for alpha characters.

Anonymous
Not applicable
550 Views
10 Replies
Message 1 of 11

Testing a string for alpha characters.

Anonymous
Not applicable
In max 2009 I am trying to test the characters in the string "RAL" whether they are alpha characters. Using the script below

for a = 1 to "RAL".count do
(
if ((substring "RAL" a 1) as number) == undefined then
(
mesagebox "THIS IS AN ALPHA CHARACTER"
)
else
(
messagebox "THIS IS A NUMBER CHARACTER"
)
)

It works till I get to "L" which doesn't produce undefined but rather 0L. I understand this has to do with bits. Is there a way around this or possibly another solution?

Thanks in advance
0 Likes
551 Views
10 Replies
Replies (10)
Message 2 of 11

Anonymous
Not applicable
I don't see any problems in the script. But you should use the right quotation marks, and not the one from wordpad, so that MaxScript doesn't produce errors. This works good for me:
for a = 1 to "RAL1".count do
(
if ((substring "RAL1" a 1) as number) == undefined then
(
messagebox "THIS IS AN ALPHA CHARACTER"
)
else
(
messagebox "THIS IS A NUMBER CHARACTER"
)
)
0 Likes
Message 3 of 11

Anonymous
Not applicable
In Max 7

"L" as number produces undefined

In Max 2009

"L" as number produces 0L
0 Likes
Message 4 of 11

Steve_Curley
Mentor
Mentor

(

fn isAlpha inChar =
(
inByte = bit.charAsInt inChar
if &#40;&#40;&#40;inbyte >= 65&#41; and &#40;inByte <= 90&#41;&#41; or &#40;&#40;inByte >= 97&#41; and &#40;inByte <= 122&#41;&#41;&#41; then
True
else
False
&#41;

fn isDigit inChar =
&#40;
inByte = bit.charAsInt inChar
if &#40;&#40;inbyte >= 48&#41; and &#40;inByte <= 57&#41;&#41; then
True
else
False
&#41;

testString = "RAL"
for a = 1 to testString.count do
&#40;
ss = subString testString a 1
if isAlpha ss then
MessageBox "LETTER"
else
if isDigit ss then
MessageBox "NUMBER"
else
MessageBox "Something strange..."
&#41;
&#41;


Damn the code formatting on here - that should all be indented :&#40;

isalpha.zip


Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 5 of 11

Steve_Curley
Mentor
Mentor
He did use the correct quote marks, but the forum software screws with them unless they are in between CODE tags.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 6 of 11

Anonymous
Not applicable
An easier way is to test with lowercase

ss = "RAL"
for a = 1 to ss.count do
&#40;
if &#40;&#40;substring &#40;tolower ss&#41; a 1&#41; as number&#41; == undefined then
&#40;
mesagebox "THIS IS AN ALPHA CHARACTER"
&#41;
else
&#40;
messagebox "THIS IS A NUMBER CHARACTER"
&#41;
0 Likes
Message 7 of 11

Steve_Curley
Mentor
Mentor
Not if you need it to work in Max 7. toLower was new in Max 2008 &#40;or required the AVGuard extensions in earlier versions&#41;.

"as number" is for converting a numeric string to a number. eg. "123.45" as number. You're using what is effectively a "side effect" of trying to convert a non-numeric string into a number which, as you already noticed, is not consistent across versions. Probably not the best way to go about it.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 8 of 11

Anonymous
Not applicable
Whether its an numeric string, alpha string or alphanumeric string I have no idea as I am fetching the string from object names in the scene.

I only used MAX 7 to test the result. I am only using MAX 2009. Using tolower causes more problems as in &#40;&#40;tolower "S"&#41; as number&#41; produces 0s, a time value. I guess I will have to go in the direction you provided with your script. I really appreciate the effort you have put into your script. Thank you so much.
0 Likes
Message 9 of 11

Steve_Curley
Mentor
Mentor
You're welcome.

You could expand the functions &#40;add further ones&#41; to provide further information if necessary, like isLower, isUpper, isSpace etc. Depends on what you need and how far you want to go.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 10 of 11

Anonymous
Not applicable
Time to learn the bit syntax. I've added the two functions to my library and I can see more use of them in the future. Thanks again.
0 Likes
Message 11 of 11

paulneale
Advisor
Advisor
Does this work for you?

numbers=#&#40;"1","2","3","4","5","6","7","8","9","0"&#41;
str="R1A2L3"
for i = 1 to str.count do
&#40;
if findItem numbers str == 0 then
&#40;
format "%: IS AN ALPHA CHARACTER\n" str
&#41;else
&#40;
format "%: IS AN NUMBER CHARACTER\n" str
&#41;
&#41;


Not sure how fast it is but if you are just going through the names of objects it shouldn't be that slow.
Paul Neale

http://paulneale.com


Paul Neale




EESignature

0 Likes