0

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:}

1
  • 1
    Sample input would be nice. Commented Dec 7, 2022 at 4:58

1 Answer 1

1
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.
Sign up to request clarification or add additional context in comments.

1 Comment

This works! I need to declare the group as a list too

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.