user204415
user204415

Reputation: 317

Remove characters at end of each element

I have a problem with removing characters in TCL.

I have a variable that contains 3 elements:

> puts $scan_ports
scan_clkvco_clk scan_clkvco_div_2_clk scan_clkin_clk

And I would like to save in another variable, those same 3 elements, but removing the _clk at the end of each element.

This is what I have tried:

regsub -all "_clk" $scan_ports "" scan_groups

But it removes all _clk matches

The expected result should be:

puts $scan_groups
> scan_clkvco scan_clkvco_div_2 scan_clkin

Upvotes: 0

Views: 531

Answers (3)

mrcalvin
mrcalvin

Reputation: 3434

Unless you haven't already subjected the values held by scan_ports to a treatment as a Tcl list, you might also want to consider mapping out the suffix using string map:

string map {"_clk " " "} "$scan_ports "

If already a list, or meant to be used as a list in a subsequent step, then Donal's solution is likely the way to go.

Upvotes: 1

Donal Fellows
Donal Fellows

Reputation: 137567

This is where lmap can help, and you really ought to be using an anchored match.

lmap sp $scan_ports {regsub {_clk$} $sp ""}

Doing it like this means that it won't do weird things with elements like foo_clk_bar_clk boo_clk.

Upvotes: 0

Jerry
Jerry

Reputation: 71538

You can use the end of word anchor:

regsub -all {_clk\M} $scan_ports "" scan_groups
# scan_clkvco scan_clkvco_div_2 scan_clkin

Note: it's usually also better to use braces with re's.

Upvotes: 0

Related Questions