Tcl_ListObj(3tcl)
β¨ NAME β¨
ok so bestie, this is a page from the Tcl Library Procedures manual, and it's all about manipulating Tcl values as lists π
The main characters are Tcl_ListObjAppendList, Tcl_ListObjAppendElement, Tcl_NewListObj, Tcl_SetListObj, Tcl_ListObjGetElements, Tcl_ListObjLength, Tcl_ListObjIndex, and Tcl_ListObjReplace β they manipulate Tcl values as lists π
π SYNOPSIS π
#include <tcl.h>
int
Tcl_ListObjAppendList(interp, listPtr, elemListPtr)
int
Tcl_ListObjAppendElement(interp, listPtr, objPtr)
Tcl_Obj *
Tcl_NewListObj(objc, objv)
Tcl_SetListObj(objPtr, objc, objv)
int
Tcl_ListObjGetElements(interp, listPtr, objcPtr, objvPtr)
int
Tcl_ListObjLength(interp, listPtr, lengthPtr)
int
Tcl_ListObjIndex(interp, listPtr, index, objPtrPtr)
int
Tcl_ListObjReplace(interp, listPtr, first, count, objc, objv)
π ARGUMENTS π
Tcl_Interp *interp(in) β so if something goes sideways while a value is getting converted into a list value, the error message gets parked in the interpreter's result valueβ¦ unlessinterpisNULL, in which case it just vibes elsewhere π«Tcl_Obj *listPtr(in/out) β points at the list value we're messing with. And iflistPtrisn't already pointing at a list value? bestie, an attempt will be made to convert it into one. no cap π₯Tcl_Obj *elemListPtr(in/out) β forTcl_ListObjAppendList, this points at a list value holding the elements that are gonna get appended ontolistPtr. every single element of*elemListPtrbecomes a brand new element oflistPtrβ¨ and if*elemListPtris notNULLand isn't already pointing at a list value, an attempt will be made to convert it to one.Tcl_Obj *objPtr(in) β forTcl_ListObjAppendElement, this points at the Tcl value that's getting appended tolistPtr. ForTcl_SetListObj, it points at the Tcl value that's getting converted into a list value containing theobjcelements of the array referenced byobjvπ¦Tcl_Size | int *objcPtr(in) β points at the location whereTcl_ListObjGetElementsstores the number of element values inlistPtr. May be(Tcl_Size *)NULLwhen it's not being used. If it points at a variable whose type is notTcl_Size, a compiler warning will be generated π If your extensions is compiled with-DTCL_8_API, this function will returnNULLfor lists with more thanINT_MAXelements (which should trigger proper error-handling), otherwise expect it to crash πTcl_Obj ***objvPtr(out) β a location whereTcl_ListObjGetElementsstores a pointer to an array of pointers to the element values oflistPtrπTcl_Size objc(in) β the number of Tcl values thatTcl_NewListObjwill insert into a new list value, and thatTcl_ListObjReplacewill insert intolistPtr. ForTcl_SetListObj, it's the number of Tcl values to insert intoobjPtrπTcl_Obj *const objv[](in) β an array of pointers to values.Tcl_NewListObjwill insert these values into a new list value, andTcl_ListObjReplacewill insert them into an existinglistPtr. Each value will become a separate list element πTcl_Size | int *lengthPtr(out) β points at the location whereTcl_ListObjLengthstores the length of the list. May be(Tcl_Size *)NULLwhen it's not being used. If it points at a variable whose type is notTcl_Size, a compiler warning will be generated π If your extensions is compiled with-DTCL_8_API, this function will returnTCL_ERRORfor lists with more thanINT_MAXelements (which should trigger proper error-handling), otherwise expect it to crash πTcl_Size index(in) β the index of the list element thatTcl_ListObjIndexis supposed to return. The first element has index0βTcl_Obj **objPtrPtr(out) β points at the place whereTcl_ListObjIndexis to store a pointer to the resulting list element value πTcl_Size first(in) β index of the starting list element thatTcl_ListObjReplaceis to replace. The list's first element has index0πΈTcl_Size count(in) β the number of elements thatTcl_ListObjReplaceis to replace π§
(part 1 energy only β part 2 is coming, don't go anywhere)
π DESCRIPTION π
ok so bestie, here's the tea: Tcl list values have an internal representation that supports the efficient indexing and appending. The procedures described in this man page are used to create, modify, index, and append to Tcl list values from C code. yes, from C. we're doing real work today π
π«Ά the append girlies
Tcl_ListObjAppendList and Tcl_ListObjAppendElement both add one or more values to the end of the list value referenced by listPtr.
Tcl_ListObjAppendListappends each element of the list value referenced byelemListPtrTcl_ListObjAppendElementappends the single value referenced byobjPtr
Both procedures will convert the value referenced by listPtr to a list value if necessary. If an error occurs during conversion, both procedures return TCL_ERROR and leave an error message in the interpreter's result value if interp is not NULL.
Similarly, if elemListPtr does not already refer to a list value, Tcl_ListObjAppendList will attempt to convert it to one, and if an error occurs during conversion, will return TCL_ERROR and leave an error message in the interpreter's result value if interp is not NULL.
Both procedures invalidate any old string representation of listPtr and, if it was converted to a list value, free any old internal representation. Similarly, Tcl_ListObjAppendList frees any old internal representation of elemListPtr if it converts it to a list value. After appending each element in elemListPtr, Tcl_ListObjAppendList increments the element's reference count since listPtr now also refers to it. For the same reason, Tcl_ListObjAppendElement increments objPtr's reference count. If no error occurs, the two procedures return TCL_OK after appending the values. slay β¨
πΈ making and remaking lists
Tcl_NewListObj and Tcl_SetListObj create a new value or modify an existing value to hold the objc elements of the array referenced by objv, where each element is a pointer to a Tcl value.
- If
objcis less than or equal to zero, they return an empty value. - If
objvisNULL, the resulting list contains 0 elements, with reserved space in an internal representation forobjcmore elements (to avoid its reallocation later).
The new value's string representation is left invalid. The two procedures increment the reference counts of the elements in objc since the list value now refers to them. The new list value returned by Tcl_NewListObj has reference count zero. periodt π
π¦ getting the elements out
Tcl_ListObjGetElements returns a count and a pointer to an array of the elements in a list value. It returns the count by storing it in the address objcPtr. Similarly, it returns the array pointer by storing it in the address objvPtr. The memory pointed to is managed by Tcl and should not be freed or written to by the caller.
- If the list is empty,
0is stored atobjcPtrandNULLatobjvPtr. - If
objcPtrpoints to a variable of typeintand the list contains more than2**31elements, the function returnsTCL_ERROR.
If listPtr is not already a list value, Tcl_ListObjGetElements will attempt to convert it to one; if the conversion fails, it returns TCL_ERROR and leaves an error message in the interpreter's result value if interp is not NULL. Otherwise it returns TCL_OK after storing the count and array pointer. iconic behavior honestly π
π length, bestie
Tcl_ListObjLength returns the number of elements in the list value referenced by listPtr. If the value is not already a list value, Tcl_ListObjLength will attempt to convert it to one; if the conversion fails, it returns TCL_ERROR and leaves an error message in the interpreter's result value if interp is not NULL. Otherwise it returns TCL_OK after storing the list's length. simple, clean, no drama πΈ
π index girlies, listen up
The procedure Tcl_ListObjIndex returns a pointer to the value at element index in the list referenced by listPtr. It returns this value by storing a pointer to it in the address objPtrPtr. If listPtr does not already refer to a list value, Tcl_ListObjIndex will attempt to convert it to one; if the conversion fails, it returns TCL_ERROR and leaves an error message in the interpreter's result value if interp is not NULL.
If the index is out of range β that is, index is negative or greater than or equal to the number of elements in the list β Tcl_ListObjIndex stores a NULL in objPtrPtr and returns TCL_OK. Otherwise it returns TCL_OK after storing the element's value pointer.
β οΈ okay this part MATTERS, do not skip it: the reference count for the list element is not incremented; the caller must do that if it needs to retain a pointer to the element, or "bounce" the reference count when the element is no longer needed. This is because a returned element may have a reference count of 0. Abstract Lists create a new element Obj on demand, and do not retain any element Obj values. Therefore, the caller is responsible for freeing the element when it is no longer needed. Note that this is a change from Tcl 8 where all list elements always have a reference count of at least 1. (See ABSTRACT LIST TYPES, STORAGE MANAGEMENT OF VALUES,Tcl_BounceRefCount(3tcl), andlseq(3tcl)for more information.)
π this is the part where you actually have to pay attention, i know. i know. but you got this πͺ
π replacing elements
Tcl_ListObjReplace replaces zero or more elements of the list referenced by listPtr with the objc values in the array referenced by objv. If listPtr does not point to a list value, Tcl_ListObjReplace will attempt to convert it to one; if the conversion fails, it returns TCL_ERROR and leaves an error message in the interpreter's result value if interp is not NULL. Otherwise, it returns TCL_OK after replacing the values.
okay so the rules of the game:
- If
objvisNULL, no new elements are added. - If the argument
firstis zero or negative, it refers to the first element. - If
firstis greater than or equal to the number of elements in the list, then no elements are deleted; the new elements are appended to the list. countgives the number of elements to replace.- If
countis zero or negative then no elements are deleted; the new elements are simply inserted before the one designated byfirst.
Tcl_ListObjReplace invalidates listPtr's old string representation. The reference counts of any elements inserted from objv are incremented since the resulting list now refers to them. Similarly, the reference counts for any replaced values are decremented. balanced, as all things should be βοΈβ¨
π the multitool era
Because Tcl_ListObjReplace combines both element insertion and deletion, it can be used to implement a number of list operations. For example, the following code inserts the objc values referenced by the array of value pointers objv just before the element index of the list referenced by listPtr:
result = Tcl_ListObjReplace(interp, listPtr, index, 0,
objc, objv);
Similarly, the following code appends the objc values referenced by the array objv to the end of the list listPtr:
result = Tcl_ListObjLength(interp, listPtr, &length);
if (result == TCL_OK) {
result = Tcl_ListObjReplace(interp, listPtr, length, 0,
objc, objv);
}
The count list elements starting at first can be deleted by simply calling Tcl_ListObjReplace with a NULL objvPtr:
result = Tcl_ListObjReplace(interp, listPtr, first, count,
0, NULL);
ate π₯ one function doing insert, append AND delete? it's giving main character energy fr fr
π REFERENCE COUNT MANAGEMENT π
this section is the vibe check for refcounts, bestie. pay attention π«Ά
Tcl_NewListObj always returns a zero-reference object, much like Tcl_NewObj. If a non-NULL objv argument is given, the reference counts of the first objc values in that array are incremented.
Tcl_SetListObj does not modify the reference count of its objPtr argument, but does require that the object be unshared. The reference counts of the first objc values in the objv array are incremented.
Tcl_ListObjGetElements, Tcl_ListObjIndex, and Tcl_ListObjLength do not modify the reference count of their listPtr arguments; they only read. Note however that these three functions may set the interpreter result; if that is the only place that is holding a reference to the object, it will be deleted. π (yes, reading can be lethal, that's just how it is)
Tcl_ListObjAppendList, Tcl_ListObjAppendElement, and Tcl_ListObjReplace require an unshared listPtr argument. Tcl_ListObjAppendList only reads its elemListPtr argument. Tcl_ListObjAppendElement increments the reference count of its objPtr on success. Tcl_ListObjReplace increments the reference count of the first objc values in the objv array on success. Note however that all these three functions may set the interpreter result on failure; if that is the only place that is holding a reference to the object, it will be deleted.
π "unshared" is not a suggestion. it's a requirement. do not come crying to me later.
π SEE ALSO π
go read those too, i'm not doing your homework for you π
π·οΈ KEYWORDS π·οΈ
append, index, insert, internal representation, length, list, list value, list type, value, value type, replace, string representation
and that's the whole page bestie!! πβ¨ this one's from Tcl 8.0 and it lives at Tcl_ListObj(3tcl) β go forth and manipulate those list values, you absolute icon ππ
π¦ where this page comes from
- ubuntu questing: tcl9.0-doc 9.0.2+dfsg-1
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 1996-1997 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms