0

I am new to tcl. I have some doubt regarding can i loop a variable in tcl.

I have set variable called handle i.e

set handle [pcap open -ip 192.168.1.3] 

This creates a handle pcap0. I wanna know whether can i loop this variable in tcl so that at a time i can create a ten handles.

2 Answers 2

2

You have two options: lists and arrays.

1. Using a list

In Tcl, a list is a value that contains a sequence of other (arbitrary) values. You can add things to the end of a list in a variable with lappend, and iterate over the list with foreach.

foreach ipaddress {192.168.1.3 192.168.1.4 192.168.1.5 ...} {
    lappend handles [pcap open -ip $ipaddress]
}

foreach handle $handles {
    # Do something with $handle here
}

2. Using an array

In Tcl, an array is a composite variable backed by an associative map (you look things up by any value you want, not necessarily a number). They can work quite well with lists.

set the_addresses {192.168.1.3 192.168.1.4 192.168.1.5 ...}
foreach ipaddress $the_addresses {
    set handle($ipaddress) [pcap open -ip $ipaddress]
}

foreach ipaddress $the_addresses {
    # Do something with $handle($ipaddress) here
}

Which option is best depends on the details of what you are doing.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply . I am well aware for this syntax this means i need to define all the ip in list or array.In my case i am creating 10 handles with same IP i.e 192.168.1.4 .Can you suggest me any syntax or proc in tcl.
0

Donal gave what seemed to be the complete answer, if the IP address is the same, simply do:

set handles ""
set ipaddress 192.168.1.3
for {set i 0} {$i < 10} {incr i} {
    lappend handles [pcap open -ip $ipaddress]
}

foreach handle $handles {
    # Do something with $handle here
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.