Reputation:
I am creating an array in TCL for which the input should be a series of numbers. The first two elements of the array is supposed to carry the information for deletion of the elements present inside the array. First element must carry the index of the element occupying the array where the deletion should start with and the second element carry the index of the element occupying the array up to which the deletion should progress.
I am kind of new to TCL and tried the below code but facing errors saying cant read array (numarray here). Some help in fixing it is most welcome.
#! /user/bin/tclsh
puts "Enter sequence count: ";
gets stdin count;
puts "\nEnter the numbers: ";
for {set i 0} {$i < $count} {incr i} {
gets stdin numarray($i);
}
set delstart $numarray(0);
puts "Starting index: " $numarray(0);
set delend $numarray(1);
for {set $i $delstart} {$i < $delend} {incr $i} {
unset numarray($i);
}
puts "\nNumber array after deletion :";
foreach $i [array names numarray] {
puts "$numarray($i)";
}
Upvotes: 0
Views: 1228
Reputation: 5723
You have multiple problems throughout the code due to one underlying issue.
Generally speaking, when you want the value of a variable, use the $
in front.
When you are writing to a variable, use the variable name.
I will use one line from your code as an example:
for {set $i $delstart} {$i < $delend} {incr $i} {
As it is, the command would be interpreted as:
for {set 12 $delstart} {$i < $delend} {incr 12} {
12
is a valid variable name, but not the one you are using.
Should be:
for {set i $delstart} {$i < $delend} {incr i} {
Only the test $i < $delend
needs the value.
Upvotes: 1