0

My TCL Script is as follows,

set a 1
while {$a < 6} {
    set interface [list ge1 vlan$a ge2 vlan$a ge3 vlan$a]
    foreach each $interface {
    puts $each
    }
incr a
}

Now I am getting output as shown below :

 C:\Desktop>tclsh loop_through_same_list_increment_variable.tcl
ge1
vlan1
ge2
vlan1
ge3
vlan1
ge1
vlan2
ge2
vlan2
ge3
vlan2
ge1
vlan3
ge2
vlan3
ge3
vlan3
ge1
vlan4
ge2
vlan4
ge3
vlan4
ge1
vlan5
ge2
vlan5
ge3
vlan5

C:\Desktop>

But I am trying to get output as shown below:

ge1
vlan1
vlan2
vlan3
vlan4
vlan5
ge2
vlan1
vlan2
vlan3
vlan4
vlan5
ge3
vlan1
vlan2
vlan3
vlan4
vlan5

I want to loop only vlan's using the above TCL script.

1
  • incr a is basically used to increment the numerical values in tcl. It does not matter whether it is inside a loop or not. Your might have to change the logic in the loop. Commented Sep 12, 2014 at 15:54

1 Answer 1

1

I am not sure about your intended purpose, but this might help

#Keeping interfaces as a list
set interface [list ge1 ge2 ge3]
#Looping the each interface with vlan values
foreach ge $interface {
    puts $ge
    set a 1
    while { $a < 6 } {
      puts vlan$a   
      incr a
    }
}

We could have done this with another approach as mentioned by Donal in here. But, I am not sure if the number of interfaces and vlans are not same or not as per your requirement. If yes, we could have used a single foreach to cover up to whole output instead of two loops. There can be multiple solutions for this problem. This is one sample which will satisfy your needs.

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

1 Comment

for loop is equivalent: for {set a 1} {$a < 6} {incr a} {puts vlan$a}

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.