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!

regex in tcl script

Status
Not open for further replies.

apertosa

Programmer
Aug 11, 2006
15
US
Hi folks and thanks for reading.
I am a newbie in both TCL and REGEX (ugh...lots to learn), so please be forgiving.
I did my research but couldn't find a match (hehe!) of what i need to do.
The problem:
I have a file that I'm parsing line by line in TCL
The file looks like this:

'point1 ' 'left/right' 0.0 -200.0 225.0
'point2 ' 'left/right' -200.0 -400.0 225.0
...

-> a name, a string with / and three numerical values.

TCL allows me to directly call the regexp command.
I would like to store the 5 entities in appropriate variables: name, symmetry, x y and z.
Code:
   while {![eof $raw] } {
      gets $raw line
      if { [regexp \
         {  --a string---a string--- a signed float --- a signed float --- a signed float  }\
             $line name symmetry x y z ]} {
             puts $name
         }
   }
   close $raw
I have absolutely no idea on where to go from here. Can someone throw me a bone?
thanks!
Andrea
 
With someone's help I was able to get what I wanted:
here is the solution:

Code:
if {[string compare $u sub] == 0} {
   set raw [open $file r]
   while {![eof $raw] } {
      gets $raw line
      if { [regexp \
         {^\s*'([^']*)'\s+'([^']*)'\s+(-?(?!0\d)\d+\.\d*)\s+(-?(?!0\d)\d+\.\d*)\s+(-?(?!0\d)\d+\.\d*)\s*$} \
            $line hp_name hp_symmetry x y z] } {
            puts $hp_name
            puts $hp_symmetry
            puts $x
            puts $y
            puts $z 
         }
      }
   }
   close $raw
 
regexp is very powerfull and I admit, it still gives me a thrill when I get the syntax right. That said, however, Tcl lists are a more direct way to do what you want:
Code:
[red]#the following line is as you had it[/red]
set raw [open $file r]
[red]#the next line puts every line in the file in a list[/red]
set linelist [split [read $raw] /n]
close $raw
[red]loop on each line in the list[/red]
foreach line $linelist {
   [red]#Tcl will treat a string as a list so this next is technically unnecessary[/red]
   set list1 [split $line " "]
   foreach elm $list1 {puts $elm}
}

_________________
Bob Rashkin
 
OK. I follow you.
I used regex because (I think) it automatically sets the variable for the matches that it finds.
If I use your approach and do this:
Code:
set line " USAGE           =  'entity_name'"

set list1 [split $line " "]

and want to extract the string entity_name I have to remove trailing, middle and blank characters if I want to make sure that

lindex $list1 0
is USAGE and
lindex $list1 1 is =
etc...
right?

So my question is: is there a way to remove all the blank chars in one go?

Thanks!
Andrea.
 
Not so much, but it would be pretty much what you want if you omit the step I called superflous:
Code:
....
foreach line $linelist {
   foreach elm [red]$line[/red] {puts $elm}
}

_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top