How to extract Integer and character seperately from char variable?

rajendran

Member
I have tried with the below code.But it will fail if we have multiple words in the character field.

Code:
def var i as char format "x(30)" no-undo.
def var v as char format "x(30)" no-undo.
def var b as int no-undo.
def var c as int no-undo.
def var v-cleanstring as char no-undo.

set i.
v= substring(i,index(i," ",1),length(i)) no-error.
message "1" v.
b = int(v) no-error.
if error-status:error then do:
   v = substring(i,1,index(i," ",1)) no-error.
   c= int(v) no-error.
   if error-status:error then
      leave.
   else
      v-cleanstring = replace(i,v,"") no-error.
     message "3" v-cleanstring.
   end.
else do:
   v-cleanstring = replace(i,v,"") no-error.
   message "4" v-cleanstring.
end.
message "5" v v-cleanstring.
The values for i could be like "abcde 12345" or "12345 abcde" or abcde efgh 12345" or "12345 abcd efgh".Please let me know how to extract interger and character(alphabets) separately from the character variable.
 
Last edited by a moderator:

RealHeavyDude

Well-Known Member
Please always wrap sample code in CODE tags.

You could try something like this given that the string will only contain 1 "integer" word:

Code:
define variable testString  as character  no-undo initial "abc 4711 defg ztut".
define variable entryNo  as integer  no-undo.
define variable myInteger  as integer  no-undo initial ?.
/* Split the string into words */
do entryNo = 1 to num-entries ( testString, " " ):
  /* Check each word whether is is a valid integer */
  assign myInteger = integer ( entry ( entryNo, testString, " " ) ) no-error.
  /* Got you */
  if myInteger > 0 then do:
    message 'integer value: ' myInteger view-as alert-box info.
    leave.
  end.
end.
Heavy Regards, RealHeavyDude.
 

Cringer

ProgressTalk.com Moderator
Staff member
not tested but I'd do something like
Code:
def var lv-source as char. 
def var lv-char as char.
def var lv-int as char. 
def var lv-intresult as int. 
def var lv-tempint as int. 
def var lv-i as int. 

do lv-i = 1 to length(lv-Source):
  assign 
    lv-Tempint = int(substring(lv-Source,lv-i,1)) no-error. 
  if error-status:error then 
    lv-char = lv-char + substring(lv-Source,lv-i,1).
  else 
    lv-int = lv-int + substring(lv-Source,lv-i,1).
end. 

lv-intresult = int(lv-int). 

message lv-intresult skip lv-char view-as alert-box.
 

rajendran

Member
Thanks RHD and cringer.But i couldn.t get 0 if its prefixed before any integer word like 09877464 and it returns only 9877464.
 
rajendran,

The results are the same and the code is parsing correctly. If you need to see leading zero's then you would need to format the display. Something like: display lv-intresult format "999999999999".

Rod
 
Top