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

substring in TCL 1

Status
Not open for further replies.

birdy1980

Programmer
Nov 1, 2011
9
0
0
GB
Hi

Is there an easy way to perform substring operations in TCL

For eg if I have a file with something like

# Ingore this
#ignore
a1:a2:a3
b1:b2:b3

And
1. I want to ignore the lines with #
2. I want to pick each value between the ':' for each line and assign them to different variables
i.e. set var1 = a1
set var2 = a2
set var3 = a3
....etc
within a loop for every line

Is this possible
 
Hi

birdy1980 said:
Is there an easy way to perform substring operations in TCL
[tt]string range[/tt]

As requested, however I would skip the part with the separate var1..var3 variables :
Code:
[b]set[/b] [b]file[/b] [teal][[/teal][b]open[/b] [green][i]"birdy1980.txt"[/i][/green][teal]][/teal]

[b]while[/b] [teal]{[/teal][teal][[/teal][b]gets[/b] [navy]$file[/navy] line[teal]]!=-[/teal][purple]1[/purple][teal]}[/teal] [teal]{[/teal]
  [b]if[/b] [teal]{[/teal][teal][[/teal][b]string[/b] range [navy]$line[/navy] [purple]0[/purple] [purple]0[/purple][teal]]==[/teal][green][i]"#"[/i][/green][teal]}[/teal] [teal]{[/teal] [b]continue[/b] [teal]}[/teal]
  [b]set[/b] part [teal][[/teal][b]split[/b] [navy]$line[/navy] [green][i]":"[/i][/green][teal]][/teal]
  [b]set[/b] var1 [teal][[/teal][b]lindex[/b] [navy]$part[/navy] [purple]0[/purple][teal]][/teal]
  [b]set[/b] var2 [teal][[/teal][b]lindex[/b] [navy]$part[/navy] [purple]1[/purple][teal]][/teal]
  [b]set[/b] var3 [teal][[/teal][b]lindex[/b] [navy]$part[/navy] [purple]2[/purple][teal]][/teal]
[teal]}[/teal]

[b]close[/b] [navy]$file[/navy]


Feherke.
 
Hi Feherke

That was quick! wow.

The reason for setting to different variables is because we are planning to read a file which has such a format to get details of parameters which will be used in the script.
That was the only reason.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top