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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace techniques

Status
Not open for further replies.

cncprogrammer

Programmer
Mar 16, 2004
4
US
Can I replace every instance of a character in a given string with a space? i.e. 2004_THIS_IS_A_STRING where literal output is: 2004 THIS IS A STRING. Input will always be uppercase or numeric only.

Working on a post processor.
 
Use this:

Code:
set x "THIS_IS_A_STRING"
set removed [string map {_ " "} $x]

now $removed contains
THIS IS A STRING

Greetings,
Tobi
 
Tobi,

Thank you. I will run it in the morning. Just learning tcl to modify a tcl based post processor for UniGraphics NX, CAD/CAM software. U/G furnishes U/G Post Builder software but you still have to stick your fingers in the code.

Will you define: map {_ " "}

I'm assuming string is causing the input to be acted on as literal.

Also, I noticed your input string is enclosed in double quotes. Is this required? When I set x the input will always be without them. I will be setting it to another varaible.

Will this be a legal argument inside of another? For instance run a proc each occurance, a sub-program if you will? I'm writing these names on the fly after compiling a stack, reading it and pulling data for a template to write into. Also, after the lappend I am required to process the stack again for complete file output.


 
The Tcl help files are your best friend.
Under "string" it will show:
SYNOPSIS
string option arg ?arg ...?
DESCRIPTION
string bytelength string
string compare ?-nocase? ?-length int? string1 string2
string equal ?-nocase? ?-length int? string1 string2
string first string1 string2 ?startIndex?
string index string charIndex
integer
end
end-integer
string is class ?-strict? ?-failindex varname? string
alnum
alpha
ascii
boolean
control
digit
double
false
graph
integer
lower
print
punct
space
true
upper
wordchar
xdigit
string last string1 string2 ?lastIndex?
string length string
string map ?-nocase? charMap string
string match ?-nocase? pattern string
*
?
[chars]
\x
string range string first last
string repeat string count
string replace string first last ?newstring?
string tolower string ?first? ?last?
string totitle string ?first? ?last?
string toupper string ?first? ?last?
string trim string ?chars?
string trimleft string ?chars?
string trimright string ?chars?
string wordend string charIndex
string wordstart string charIndex
Then, under "string map":
string map ?-nocase? charMap string
Replaces characters in string based on the key-value pairs in charMap. charMap is a list of key value key value ... as in the form returned by array get. Each instance of a key in the string will be replaced with its corresponding value. If -nocase is specified, then matching is done without regard to case differences. Both key and value may be multiple characters. Replacement is done in an ordered manner, so the key appearing first in the list will be checked first, and so on. string is only iterated over once, so earlier key replacements will have no affect for later key matches. For example,
string map {abc 1 ab 2 a 3 1 0} 1abcaababcabababc

will return the string 01321221.

You would only need the ""s to define your input string if there were any embedded spaces.

Bob Rashkin
rrashkin@csc.com
 
Bob,

Thanks again for the help. I tried the command but my software says map was illegal. S/W is based on 8.0. What I ended up getting to work was regsub

I used:

Code:
regsub -all {_} $my_varaible " " my_variable

I've come up with a couple of clunky ways to trim the last four out of a string. In this application the string always ends in 8 numbers of which I want the last four. I don't quite get the trim. Can I not cut out the last 4 and put them in a vaiable without several arguments?
 
Regsub is good but slower than map. Unfortunately, I think 8.0 is too early for map. I think it was introduced in 8.2.

Bob Rashkin
rrashkin@csc.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top