ARRAY in TCL

What is Array?

During execution, we can have a few variables which we need. So we can take these variables in the form of array in tcl.

set array(index) value ;

The way we set variables, in the same way we set an array. The only difference is that it comes with a pattern of variables.

set numbers (0) 10 ;
set numbers (1) 20;
puts $numbers (0)
puts $numbers (1)

Result : 10
20

Size of Array

[array size variable_name]

set numbers (0) 10 ;
set numbers (1) 20;

[array size numbers]

Results : 2

We can have conditional statements and loops in array.

set numbers (0) 10;
set numbers (1) 20;

for {set index 0} {$index < [array size numbers]} {incr index} {
puts “numbers($index) : $numbers ($index)”
}

Result: number (0) : 10
number (1) : 20

Similar to ‘for’ loop, we can use foreach loop as well.

set numbers (0) 10;
set numbers (1) 20;

foreach index [array names numbers] {
puts “numbers($index) : $numbers ($index)”
}

Result: number (0) : 10
number (1) : 20

Admin
Admin

Leave a Reply