0

Here are the results of some TCL commands.

get_props -type assert
{"a", "b", "c", "d"}

Now all these 4 objects have certain attributes associated with them. But I am interested in the "enabled" attribute only.

get_attribute [get_props a] enabled
true

get_attribute [get_props b] enabled
false

get_attribute [get_props c] enabled
true

get_attribute [get_props d] enabled
false

Now I want to convert only "enabled" objects (enabled = true) out of these 4 "assert" type objects into "cover" type objects (So only "a" & "c" should be converted) and for converting "assert" into "cover", the command is fvcover.

I have tried the following command:

fvcover [get_props -type assert]

Now the problem is that, this fvcover command converts all 4 "assert" type objects into "cover" type objects, instead of just "a" & "c".

So I guess, I need to combine both get_props & get_attributes command, but I don't know how to do it.

So how to solve this problem?

Note :- "a", "b", "c", "d" are just for explanation. In reality, get_props command may return any number of results with any name. But out of that list, I need to convert, only those objects, whose "enabled" attribute is true.

Karan Shah
  • 1,912
  • 1
  • 29
  • 42

1 Answers1

0

The lists are not in Tcl format. Here's some test code you can use to convert from your format to Tcl.

#### PROCS FOR TESTING ####
proc get_props {type {assert no}} {
    if {$type == "-type" && $assert == "assert"} {
        return {"a", "b", "c", "d"}
    }
    if {$type == "a" || $type == "c"} {
        return [list enabled true]
    } elseif {$type == "b" || $type == "d"} {
        return [list enabled false]
    }
    return [list NOT FOUND]
}

proc get_attribute {a k} {
    foreach {key value} $a {
        if {$key == $k} {
            return $value
        }
    }
    return NOT_FOUND
}


# get props. props is in a list format that is not native tcl list
set props [get_props -type assert]

# convert props to tcl list
set props_list [string map {, ""} $props]

# make a list to catch enabled props
set enabled_props [list]

# add enabled props to new list
foreach {prop_name} $props_list {
    if {[get_attribute [get_props $prop_name] enabled] == "true"} {
        lappend enabled_props "\"$prop_name\""
    }
}
# convert enabled_props to your format
set enabled_props "{[join $enabled_props ", "]}"

# run your program on $enabled_props

puts $enabled_props
wolfhammer
  • 2,641
  • 1
  • 12
  • 11
  • Why have you defined get_props & get_attribute methods, when both are in fact commands(not tcl commands but both are commands to the shell, which executes this tcl) and we can use them directly. – Karan Shah Dec 09 '15 at 03:24
  • I don't have the same software that you have so I made the procs for testing purposes. The input and output should be the same as what you posted. The enabled_props should be in the same format that your software uses. Try the code without the test procs. – wolfhammer Dec 09 '15 at 19:00
  • Without test procs, I tried the code, but I was getting blank lists in enabled_props variable. – Karan Shah Dec 10 '15 at 10:34
  • The example data might not be in the correct format. Can you provide the data structure for `get_props -type assert`? – wolfhammer Dec 10 '15 at 13:16