Cisco Support Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Announcements

Welcome to Cisco Support Community. We would love to have your feedback.

For an introduction to the new site, click here. And see here for current known issues.

New Member

Using foreach in TCL - efficient or not?

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?

Everyone's tags (3)
2 REPLIES
Cisco Employee

Using foreach in TCL - efficient or not?

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.

New Member

Using foreach in TCL - efficient or not?

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! :-)

545
Views
0
Helpful
2
Replies
CreatePlease login to create content