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

string map

Status
Not open for further replies.

yodaa

Programmer
Jun 10, 2004
64
SE
Hi,

I want to use string map to eliminate a set of characters from a string. The thing is that the set of characters are saved into a variable and I haven't come up with a solution to remove these from the string.

Some code that explains the problem:

Code:
set data "foo"

set form_tag(foo_name)
set form_tag(foo_date)
set form_tag(foo_id)

foreach form_variable [array names form_tag] {
    set result [string map {$data "" _ ""} form_variable]
    #Do some stuff using $result
    #...
}

I would like the result to be
--->name
--->date
--->id

It works fine to remove the "_" character, but I need to remove the "foo" as well. It seems that the string map tries to remove a set of characters that is "$data" from the string and not the value of data.

Does anybody have a tip or solution?
 
Not very elegant but:

set cmap $data
append cmap " \"\" _ \"\""

then:
set result [string map $cmap $form_variable]

Bob Rashkin
rrashkin@csc.com
 
Thansk for the reply Bob,

But the code generates an error and I don't know how to solve it. The error message is: "Error: char map list unbalanced".

What might be wrong?

If I use your code and modify
Code:
set result [string map $cmap $form_variable]
to
Code:
set result [string map {$cmap ""} $form_variable]
the error disappears but the string is not affected at all by the code.

Is there some other way of removing $foo from $form_variable?

Have tried the different string trim commands without success.
 
Solved it, not so pretty, but it works..

Code:
proc blabla {data} {
  append data "_"

  set map {}
  lappend map $tag
  lappend map ""

  set result [string map $map $form_variable]
}

Bob, thanks for the help, it led me into a solution!
 
this is how to do it:
Code:
package require textutil

array set form_tag {
 foo_name blablabla
 foo_date joebloggs
 foo_id etcaetera
}
foreach i [array names form_tag] {
  puts [::textutil::trimleft $i {^([[:alnum:]_]{3,})[_]}]
}

Voila :D
 
Is textutil a part of tcl/tk or is it a separate package that needs to be downloaded and installed?
 
basically, use the textutil package.
it is quite usefull for "regexped triming"...
 
it is part of ActiveTcl package.
But you can find it on the net very easily.
However, just try to do a "package require textuil" before downloading it...
 
actually:

Code:
package require textutil

set data "foo"
array set form_tag {
 foo_name blablabla
 foo_date joebloggs
 foo_id etcaetera
}

foreach i [array names form_tag] {
 set result [::textutil::trimleft $i "($data)_"]
}
is the correct way to do what you initially wanted to do
 
flOra, found it in tcllib1.6 on the network at work.

Didn't know it existed..

Will use it..

Thanx alot for all replies!
 
good!
tcllib is full of very usefull things such as textutil.
You can always find work-around for not using tcllib. but is is a bit useless since most of the thing you want are going to be already implemented within tcllib.

Hope this will really help your life as a Tcl/Tk developper.
 
Maybe I'm missing something here, but couldn't you just do:

set result [lindex [split $form_variable "_"] 1]

in your loop?
 
yes indeed...
as far as the foo element he/she wants to remove does not contains "_"

you method, should be faster at run time as well
 
Good call...

How about:

set result [string trim $form_variable $foo_element]

where foo_element can have any number of "_"

Just a thought...
 
Hey all,

Thanks for all the tips.. I've been using the textutil in tcllib1.6 to solve this problem.. Works perfectly..

Thanx again, the help received in this forum is indeed invaluable..
 
goodegod
don't if it will work, but if it does it might as well removed $foo_element from the end of the string.
in that case to be sure it would be better to use string trimleft.

the good point about the ::textutil::trimleft is that you can use regexp.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top