Jump to content
UBot Underground

Converting Numeric String To Integer


Recommended Posts

I'm trying to iterate over a string and distinguish between numerals and letters, such as in "abcd123efg"

 

My first attempt was:

set(#x,"abcd123efg","Global")
add list to list(%chars,$list from text($character list(#x),$new line),"Delete","Global")
set(#numbers,0,"Global")
set(#letters,0,"Global")
with each(%chars,#y) {
    if($comparison($is number(#y),"= Equals",$true)) {
        then {
            increment(#numbers)
        }
        else {
            increment(#letters)
        }
    }
}

however the debugger revealed that #letters was being incremented for every character. Thinking that perhaps I need to convert the string representation to an integer, I wrote a small function in python to do so:

set(#x,"abcd123efg","Global")
add list to list(%chars,$list from text($character list(#x),$new line),"Delete","Global")
set(#numbers,0,"Global")
set(#letters,0,"Global")
with each(%chars,#y) {
    set(#y,$ConvertStringToInt(#y),"Global")
    if($comparison($is number(#y),"= Equals",$true)) {
        then {
            increment(#numbers)
        }
        else {
            increment(#letters)
        }
    }
}
define $ConvertStringToInt(#string_char) {
    set(#char_to_return,$run python with result("def convertStringToInt(string):
    if string.isdigit():
        return int(string)
    else:
        return string
convertStringToInt(\"{#string_char}\")"),"Global")
    return(#char_to_return)
}

However I'm getting the same result; the $is_number comparison only returns FALSE.

 

Any ideas how I might accomplish this?

Link to post
Share on other sites

$is number is returning "True" and you are checking for "true" (I know it shouldn't be like that maybe open a tracker report)

 

Try this:

set(#x,"abcd123efg","Global")
add list to list(%chars,$list from text($character list(#x),$new line),"Delete","Global")
set(#numbers,0,"Global")
set(#letters,0,"Global")
with each(%chars,#y) {
    if($is number(#y)) {
        then {
            increment(#numbers)
        }
        else {
            increment(#letters)
        }
    }
}
  • Like 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...