cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2234
Views
0
Helpful
2
Replies

Using foreach in TCL - efficient or not?

mskrobacki
Level 1
Level 1

I wrote a little EEM applet to extend CLI functionality to display BGP neighbor descriptions in "show ip bgp summary" command. The code is as follows:

event manager applet bgp_summary

event cli pattern "show ip bgp summary nice" mode "exec" enter

action 1.1 cli command "enable" action 1.2 cli command "show ip bgp summary" action 2.0 foreach line $_cli_result "\n" action 2.0.1 regexp "^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)(.*)" $line junk neighbor info action 2.0.2 if $_regexp_result eq 1 action 2.0.2.1  cli command "show ip bgp neighbors $neighbor | i  Description:" action 2.0.2.2  regexp " Description: (.*)\r\n" $_cli_result junk txtdesc action 2.0.2.3       if $_regexp_result eq 1 action 2.0.2.4              puts "$txtdesc $info" action 2.0.2.5      else action 2.0.2.6          puts "$line" action 2.0.2.7       end ! ifelse action 2.1 else action 2.2   puts "$line" action 2.3 end action 2.4 end

It works without a problem, so I decided to try converting it to TCL policy using Joseph Clarke's tool (

http://www.marcuscom.com/convert_applet/). To my surprise "foreach" construct has been converted to multiple lines of code:

         [...]

    if {[regexp -indices "\n" $_ts _loc] == 0} {

        set line $_ts

        set _ts ""

    } else {

        set _mstart [lindex $_loc 0]

        set _mend [lindex $_loc 1]

        if {$_mstart == 0} {

            set line ""

        } else {

            set line [string range $_ts 0 [expr $_mstart - 1]]

        }

        set _ts [string range $_ts [expr $_mend + 1] end]

    }

         [...]

Any ideas why this has happened? Is this preffered, more efficient way of spliting the list by a delimiter or not necessarily?

2 Replies 2

Joe Clarke
Cisco Employee
Cisco Employee

Just like Perl, there is more that one way to do something in Tcl.  Since "foreach" in Tcl doesn't support the delimeter option, the conversion script had to do something to provide the same functionality.  And since delimeter can be a string, using split is not good enough.

That makes perfect sense, I just didn't know that delimeter can be longer than one character. Thanks for prompt answer and very inspirational talk at Cisco Live! :-)