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 list elements with new string 1

Status
Not open for further replies.

fabien

Technical User
Sep 25, 2001
299
AU
Hi!

I have a list called files that contains:
/data/pau00/XX_PARADISE/lmk/xx_fl3d/dipaz.bri
/data/pau00/XX_PARADISE/lmk/xx_fl3d/dipaz.bri.meta
/data/pau00/XX_PARADISE/lmk/xx_fl3d/dipaz.bri.scaleFactor


What I want is to create a function that will replace the string a given string the each element of the list with a new string

for instance
old_name = dipaz
new_name = test
to get

/data/pau00/XX_PARADISE/lmk/xx_fl3d/test.bri
/data/pau00/XX_PARADISE/lmk/xx_fl3d/test.bri.meta
/data/pau00/XX_PARADISE/lmk/xx_fl3d/test.bri.scaleFactor


I have started doing something like this but I get no string replacement:

foreach item $files {
# replace item with new name
set new_item [string map {$old_name $new_name} $item]
puts "Old: $item New: $new_item"
}

In addition, it could be that the old string (dipaz here) exists in the file path as well and I don't want to replace it there..

Many thanks

 
I would try ...

foreach item $files {

set tempList [split $item /] # replace item with new name

set flname [lindex $tempList end] # Set flname to filename at end

# make the substitution and output if necessary
if [regsub $old_name $flname $new_name newflname] {

set new_item [join [lreplace $tempList end end $newflname] /]

puts "Old: $item, New: $new_item"

}

}
 
THanks shooder, I actually found my problem and solved it using:
set new_item [string map [list $old_name $new_name] $item]

I will use your idea of splitting the the string..

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top