Forum Discussion

Eric_Flores_131's avatar
Eric_Flores_131
Icon for Cirrostratus rankCirrostratus
Oct 17, 2013
Solved

iApp table parsing issue

According to the iApp wiki page Working with Tables it says that you can use the array set command with a lindexed value from a table variable -

array set columns [lindex $row 0]

So experimenting with this in tclsh I set a test variable -

% set test "{{
    address 10.0.0.1
    port 80
}} {{
    address 10.0.0.2
    port 80
}} {{
    address 10.0.0.3
    port 80
}}"

Now the result of [lindex $test 0] is this -

% lindex $test 0
{
    address 10.0.0.1
    port 80
}

Which is the desired result as the article explains.

However, when I try this on TCL you get an error -

% array set a1 [lindex $test 0]
list must have an even number of elements

In order to make it work I have to remove the curly brackets -

% array set a1 [string map {"{" "" "}" ""} [lindex $test 0]]
% parray a1
a1(address) = 10.0.0.1
a1(port)    = 80

Am I doing something wrong or is the article wrong ?

  • The "Working with Tables" example shows a variable $row that contains 1 row of the table. In your example, the variable $test contains the entire table. If you want a single statement that operates on $test, it would be either

    %array set a1 [lindex [lindex $test 0] 0]
    or
    %array set a1 [join [lindex $test 0]]

2 Replies

  • Fred_Slater_856's avatar
    Fred_Slater_856
    Historic F5 Account

    The "Working with Tables" example shows a variable $row that contains 1 row of the table. In your example, the variable $test contains the entire table. If you want a single statement that operates on $test, it would be either

    %array set a1 [lindex [lindex $test 0] 0]
    or
    %array set a1 [join [lindex $test 0]]

  • I see how they did it now. They are using a foreach loop which will remove the first set of curly brackets then the

    lindex $row 0
    removes the last set. When trying to use it in my iApps I am using a for loop which means I need to do the first lindex for the $test index then lindex 0 the result-

    % for {set index 0} {$index < [llength $test]} {incr index} {
            puts "----------------------------------"
            array set a1 [lindex [lindex $test $index] 0]
            parray a1
    }
    ----------------------------------
    a1(address) = 10.0.0.1
    a1(port)    = 80
    ----------------------------------
    a1(address) = 10.0.0.2
    a1(port)    = 80
    ----------------------------------
    a1(address) = 10.0.0.3
    a1(port)    = 80