harsha inuganti
harsha inuganti

Reputation: 3

how to push multiple values into an array in TCL

I am trying to append the values to an array as below and it is not appending to the value list

while {[gets $fp line] != -1} {

    if { [regexp {Path Group: (\w+)} $line all group]} {
        set hash($group) {}
 
    } elseif {[regexp {\(VIOLATED\)\s+(-[0-9]*.[0-9]*)} $line all slack]} {
        puts "slack $slack\n"
         lappend hash($group) $slack     
    }

 }

parray hash

The output of the array hash is only the last value of the iteration bit not a list

expecting

Name { X Y Z} Age {3 4 5:}

Upvotes: 0

Views: 234

Answers (1)

sharvian
sharvian

Reputation: 654

set group {}
while {[gets $fp line] != -1} {
  if { [regexp {Path Group: (\w+)} $line all group]} {
  } elseif {[regexp {\(VIOLATED\)\s+(-[0-9]*.[0-9]*)} $line all slack]} {
    puts "slack $slack\n"
    lappend hash($group) $slack     
  }
}
parray hash
  • No need to set hash($group) on each matching because this will reset the contents of hash($group).
  • In lappend hash($group), if the variable does not exist, it will be created automatically.

Upvotes: 1

Related Questions