#4788 crosstalk between arrays

obsolete: 8.5.9
closed-invalid
5
2011-02-01
2011-02-01
No

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);
}

Discussion

  • miguel sofer

    miguel sofer - 2011-02-01

    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

     
  • miguel sofer

    miguel sofer - 2011-02-01
    • status: open --> closed-invalid
     
  • Stephen Ehmann

    Stephen Ehmann - 2011-02-02

    Thanks Miguel, that clarifies things and helps us know how to handle the various types that come into our commands.

     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks