I tried like this:
Tcl_Obj *array = Tcl_NewObj();
Tcl_Obj *index = Tcl_NewStringObj("index_name", -1);
Tcl_Obj *value = Tcl_NewStringObj("element_value", -1);
Tcl_ObjSetVar2(interp, array, index, value, TCL_LEAVE_ERR_MSG);
Tcl_SetObjResult(interp, array);
return TCL_OK;
But it does not work. Any suggestions?
jnie...@kredytbank.pl (jacus) writes:
> How can I create Tcl array in C procedure,
> and how to create this array to Tcl script.
> I tried to use Tcl_ObjSetVar2(interp, part1Ptr, part2Ptr, newValuePtr, flags)
> but I do not know how to create Tcl_Obj *part1Ptr as Tcl array.
>
> I tried like this:
> Tcl_Obj *array = Tcl_NewObj();
> Tcl_Obj *index = Tcl_NewStringObj("index_name", -1);
> Tcl_Obj *value = Tcl_NewStringObj("element_value", -1);
>
> Tcl_ObjSetVar2(interp, array, index, value, TCL_LEAVE_ERR_MSG);
I expect that this will create an array with the name "" (empty
string), having one element named "index_name" and the value
"element_value". In Tcl try
parray ""
after the call to this command.
> Tcl_SetObjResult(interp, array);
> return TCL_OK;
That just returns an empty string to the caller.
If you expect to return an array from your command, that just doesn't
work. You can't do that in Tcl and you can't do that in C.
You can create an array variable as you do here, at the same level as
the caller or at the global level (TCL_GLOBAL_ONLY) or namespace level
(TCL_NAMESPACE_ONLY).
You can also return a list from your command, so that the caller can
use [array set] to create an array at his convenience. Note that
[array set] is not as costly as it might seem, because of the Tcl_Obj
reference counting mechanism.
benny
jacus
jnie...@kredytbank.pl (jacus) writes:
> I'm going to return a list of arrays from procedure. I create list
> with Tcl_NewListObj. I add element to list with
> Tcl_ListObjAppendElement. But how can I create an array to add it
> to the list?
You can't. Arrays are not Tcl objects. So you can not include arrays
in a list. You can include the name of an array in a list or you can
convert the array into a list of labels and values with [array get]
and use the that.
Terminology:
- Object - An object is a value. Examples are "abc" (a string), 1 (a
string representing a number) or {el1 el2 el3} (a string
representing a list). Internally on the C level, values are
represented as Tcl_Obj, with optimizing internal representations for
common logical types, like lists or numbers.
- Variable - A variable is a name in some scope that is either
associated with a single object (value) or the variable is an array.
- Array - An array is a variable that has multiple values,
differentiated by a key. Another way of looking at it is that an
array is a collection of variables whose names are formed like
"arrname(key)" with "arrname" identifying the array.
The important bits are: Arrays are variables, they are not values
(objects). Variables always have a name and belong to a scope. A
procedure returns a value, a list contains values. Arrays are not
values.
I hope I got all that right ;-)
benny