Skip to content
Tcl Source Code View Ticket
Login

View Ticket

crosstalk between arrays

Closed / Invalid3169173fffffffffffffffffffffffffffffffffCreated 16 years agoUpdated 16 years ago
Descriptionplain2011-02-01 02:02:39
It seems like there is some sort of cross talk between arrays.

we are trying to upgrade our system from 8.4.9 to 8.5.9 and this is a blocker.

proc test {} {
    array set foo ""
    array set bar ""

    set foo(b) a
    set bar(a) b

    foreach i [array names bar] {
        puts "$i : [otype $i]"
    }

    set l {}
    foreach {i j} [array get foo] {
       foreach k $j {
          lappend k $l
       }
    }

    foreach i [array names bar] {
        puts "$i : [otype $i]"
    }
}

test

Output:
a : string
a : list

array names is returning an element that is a list!  Does not happen if we don't use the "test" proc.

otype is a C function we wrote to print the type of
the object and is implemented like this:
   if ( obj->typePtr ) {
       char* typeName = obj->typePtr->name;
       Tcl_SetResult(interp, typeName, TCL_VOLATILE);

   } else if ( obj->bytes ) {
       Tcl_SetResult(interp, (char*)"string", TCL_STATIC);

   } else {
       Tcl_SetResult(interp, (char*)"unknown", TCL_STATIC);
   }
msoferplain2011-02-01 19:25:42
Not sure about what you are trying to do, but: the type/intrep of a Tcl_Obj is just a cache optimising the representation for the *last* usage of this obj. It is *not* really a property of the Tcl_Obj, and is subject to changing (aka shimmering).

In your script, let us follow what happens to 'a':

set foo(b) a
set bar(a) b
# 'a' is just a string

foreach i [array names bar] {
       puts "$i : [otype $i]"
}
# 'a' has only been ever used used as a string, still a string

set l {}
    foreach {i j} [array get foo] {
       foreach k $j {
          lappend k $l
       }
    }
# 'a' has been treated as a list by doing [foreach] on it; type is now list
msoferplain2011-02-01 19:25:43
allow_comments - 1
msoferplain2011-02-01 19:26:52
allow_comments - 0
sehmannplain2011-02-03 02:49:57
Thanks Miguel, that clarifies things and helps us know how to handle the various types that come into our commands.
Log in (anonymous is fine) to add a comment.