Page 1 of 1

Reading blanks in a string, replace them with zeros.

PostPosted: Wed Mar 06, 2013 4:13 am
by dbordon
Hi all
Tried to get my answer in the forum but i could not.
My question is simple, i read a PDS dataset, using SUBSTR i delete the part of a string i'm not interested in and the remaining data, wich is 4 integer bytes are written with spaces and i wanted to trasnform those blanks with zeros.

Example:
I read the following line:
xxxxx xxxxxx(1234)
do the subtream and get
1234

but... If a read xxxxx xxxxx ll xxx ( 34)
the result is
34
and i need the result as
0034

I hope to be clear in my explanation, if you need i can write the peace of program i'm using. Tried with the funtion RIGTH, with 4 bytes and filling with zeros, but does not worked.

Thanks !

Re: Reading blanks in a string, replace them with zeros.

PostPosted: Wed Mar 06, 2013 9:16 am
by NicC
How did it not work? I find RIGHT and LEFT a bit "about-face" if I do not check the syntax first in the manual. Did you try LEFT just in case? And it is SUBSTRing not SUBSTReam!

Re: Reading blanks in a string, replace them with zeros.

PostPosted: Wed Mar 06, 2013 7:23 pm
by Pedro
Try using the TRANSLATE function.

Re: Reading blanks in a string, replace them with zeros.

PostPosted: Wed Mar 06, 2013 7:34 pm
by enrico-sorichetti
str.1 = "xxxxx xxxxx(1)"
str.2 = "qwqwqw(1) zqzqzq"
str.3 = "xxxxx xxxxx(333333)"
str.4 = "(1)ryryyrryyy"

do  s = 1 to 4
    parse var str.s head "(" tokn ")" tail
    say right(s,   2) "head" ">"head"<"
    if length(tokn) <= 8 then ,
        say right(" ", 2) "tokn" left(">"tokn"<",10) ">>"right(tokn,4,"0")"<<"
    else ,
        say right(" ", 2) "tokn" ">"tokn"<" ">>"right(tokn,4,"0")"<<"
    say right(" ", 2) "tail" ">"tail"<"
end

exit

Re: Reading blanks in a string, replace them with zeros.

PostPosted: Sat Mar 09, 2013 3:16 am
by Pedro
Enrico, it is not clear what you are demonstrating with your example. I think it should process this string:
 str.5 = "xxxxx xxxxx(  34)"     

which is what the poster was asking about. The ' 34' needs to be TRANSLATEd to '0034'

Re: Reading blanks in a string, replace them with zeros.

PostPosted: Sat Mar 09, 2013 7:39 pm
by enrico-sorichetti
just change

right(tokn,4,"0")
 to
right(strip(tokn),4,"0")

and You will have the result the TS needed

You should also have noticed that in my replies I rarely stick only/simply to the lowly technicalities
but I try to show also the general approach on tackle the problems being discussed

in this case how to parse a parentheses enclosed token

and the way the TS posted the data it was not clear how many blanks had to be handled