user442920
user442920

Reputation: 845

TCL string match from file

I am trying to find a string in a file in TCL. Using the wish console, I get a successful match between two strings. When I read a string from a file and match it to its exact copy, it fails. I can see in Eclipse that the variables contain exactly the same string...that is unless there are invisible characters trailing. The following code never returns 1, even when the variables contain exactly the same strings.

set fileId [open $::InputFile "r"]
set file_data [read $fileId]
#  Process data file
set data [split $file_data "\n"]
#search for string
foreach line $data {
     set x $::StringToFind
     set y $line
    set z [string match x y]
    puts $z
     if [ string match $::StringToFind line ] {
         return 1
     }
}

Upvotes: 1

Views: 4800

Answers (1)

Hai Vu
Hai Vu

Reputation: 40773

You need to use the dollar sign on the line variable to get its value:

     if [ string match $::StringToFind $line ] {

Also, it is a good practice to quote the condition of the if command:

     if {[string match $::StringToFind $line]} {

Upvotes: 4

Related Questions