Forum Discussion

ldesfosses's avatar
Mar 26, 2018

Access a table in the implemention

Hi,

I've a table that I don't know how to access.

It's received by the Implementation, if I print it :

puts "table $mytable"

I have this :

table {{
    name lala
    nodeaddr 8.5.2.1
    port 80
}} {{
    name lolo
    nodeaddr 5.2.1.4
    port 80
}}

How can have access to it ?

If I try to list every item :

foreach item $mytable {
    puts "item{name}"
}

I have an error.

Thanks for your help.

2 Replies

  • I've written this :

     

    set tmpstring [string map {\{\{ \{} $mytable]
    set mytable [string map {\}\} \}} $tmpstring]
    foreach item $mytable {
         set nextisname 0
         set tmpitem [string trim $item]
         foreach line [split $tmpitem] {
             if { $line eq "name" } {
                 set nextisname 1
             } else { 
                 if { $nextisname eq 1 } {
                     puts "name $line"
                     set nextisname 0
                 }
             }
         }
     }

    Not my proudest code, but it allow me to access the value.

     

  • Hi dude,

    I can't see that values represent a table structure, because I think tables have just rows and columns values.

    set table {{name nodeaddr port}}
    lappend table {lala 8.5.2.1 80}
    lappend table {lolo 5.2.1.4 80}
    foreach row $table { 
        puts $row 
    }
    name nodeaddr port
    lala 8.5.2.1 80
    lolo 5.2.1.4 80
    

    So I think you need a hash with named items like this way:

    set hash "{name lala nodeaddr 8.5.2.1 port 80} {name lolo nodeaddr 5.2.1.4 port 80}"
    foreach value $hash {
        array set item $value
        puts "$item(name) | $item(nodeaddr) | $item(port)" 
        unset item
    }
    lala | 8.5.2.1 | 80
    lolo | 5.2.1.4 | 80
    

    Best Regards.