Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace white space witch one character

Status
Not open for further replies.

christaylor

Technical User
Jul 3, 2003
5
GB
I have a script which reads in data from a connection one line at a time, I then need to removed any white space (could be varying lengths) and replace it with a single character eg ";". The data is actually a list of usernames & passwords and some other info. each of the fields is seperated by white space and i need to collect each element into different variables. I can capture the data to a variable. but i have tried strok and strextract but as the white space is of different lentghs I cannot get the correct info to the other variables.
 
Probably the easiest thing to do would be to go through the string one character at a time, read each character, then copy the character to a temporary string if it is not a space (I'm assuming this is the only white space you have to deal with). If it is a space, then copy a semi-colon instead of the space. If it is the second space in a row, then don't copy it at all to the temporary string. Once you have all data copied to the temporary string, you can perform your processing on it. Here's a quick and dirty script that should work:

proc main
string sLine = "this is a test"
string sTemp
string sChar
integer iLen
integer iLoop
integer iWS

sTemp = ""
strlen sLine iLen
for iLoop = 0 upto iLen-1
substr sChar sLine iLoop 1
if strcmp sChar " "
if iWS == 1
;do nothing
else
iWS = 1
strcat sTemp ";"
endif
else
iWS = 0
strcat sTemp sChar
endif
endfor
usermsg "%s" sTemp
endproc

aspect@aspectscripting.com
 
Only problem is that the white space can include tabs or spaces. I think I can sort it out from your code above. Thanks for the quick response
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top