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!

string manipulation (ignoring portion of a string)

Status
Not open for further replies.

apertosa

Programmer
Aug 11, 2006
15
US
Hi guys,
I'm parsing a string and I'd like to extract some relevant information in a TCL.

The string:
[red]rev_lca[/red] connects [red] lower_control_arm [/red] with [red]subframe [/red] (Force) [red]2 [/red] [red] lca_front lca_rear [/red]

the portions in red is what I'm after, the rest is junk...
Now I can do this:
Code:
set joint_name [lindex $line 0]
set part_1 [lindex $line 2]
set part_2 [lindex $line 4]
set joint_loc_type  [lindex $line 6]
set joint_loc_dep1  [lindex $line 7]
set joint_loc_dep2  [lindex $line 8]

and this seems to do the trick EXCEPT... some lines might have (Revolute Joint) instead of (Force) throwing a wrench in my logic.
In other words lindex takes the blanks as the separations between the various string constituents.

How can I handle the general case?
How can I ignore whatever is contained in '('?
Or if there is a better way to do that...
Thanks a lot!
Andrea.
 
Hi

Get the last three words from the end :
Code:
set joint_name [lindex $line 0]
set part_1 [lindex $line 2]
set part_2 [lindex $line 4]
set joint_loc_type  [lindex $line [red]end-2[/red]]
set joint_loc_dep1  [lindex $line [red]end-1[/red]]
set joint_loc_dep2  [lindex $line [red]end[/red]]

Feherke.
 
alternatively, you can regsub the string before you lindex it:
Code:
regsub [i]-all[/i] {(\(\w*) (\w*\))} $line {\1_\2} line

_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top