Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TclLib 8.4 Can an array Tcl_Obj be converted to a HashTable?

42 views
Skip to first unread message

bria...@gmail.com

unread,
Aug 22, 2012, 6:29:40 AM8/22/12
to
We currently have an TCL 8.4 interpreter embedded in our C-based product to enable scripting. We already have lots of commands available to script writers. I'm adding a new command which needs to pass a collection of name-value pairs. I think 'array' would be the appropriate choice.

I'll be receiving the array as a 'Tcl_Obj * CONST objv[]'. Is there a way to convert this array to a HashTable (http://www.tcl.tk/man/tcl8.4/TclLib/Hash.htm)?

Note that I've never written C code using the TclLib. The author of this product that did all the TCL interpreter integration left the company.

Thanks,
Brian

Gerald W. Lester

unread,
Aug 22, 2012, 7:34:00 AM8/22/12
to
I'd *strongly* urge upgrading to Tcl 8.5 (should be a fairly easy upgrade)
and then use dicts for for your new command.

BTW, TclLib is all in Tcl, so to use it from C you will be using TclEval* calls.

There are several good Tcl/C consultants who could help you and your company
if you need help in the upgrade.

--
+------------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald...@kng-consulting.net |
+------------------------------------------------------------------------+

bria...@gmail.com

unread,
Aug 22, 2012, 9:22:12 AM8/22/12
to
Thanks for your response, Gerald!

> I'd *strongly* urge upgrading to Tcl 8.5 (should be a fairly easy upgrade)
> and then use dicts for for your new command.

Upgrading is a hard sell at the moment. I did notice the nice dict stuff in 8.5 but for the moment I'm stuck with 8.4's array and HashTable.

> BTW, TclLib is all in Tcl, so to use it from C you will be using TclEval* calls.

Maybe TclLib wasn't the correct name for this http://www.tcl.tk/man/tcl8.4/TclLib/Hash.htm
The area I'm adding support for the command is already C. The function declaration looks like

int some_new_command( ClientData clientData, Tcl_Interp * interp, int objc, Tcl_Obj * CONST objv[] )

My original question is still unanswered, what C data type does the 'array' map to in the Tcl_Obj objv array? Are there any library calls that convert this data type to a HashTable?

Brian

Uwe Klein

unread,
Aug 22, 2012, 9:50:41 AM8/22/12
to
bria...@gmail.com wrote:
> My original question is still unanswered, what C data type does the 'array' map to in the Tcl_Obj objv array? Are there any library calls that convert this data type to a HashTable?
>
> Brian
the tcl array object is hash table based.

note that you can only hand over the array name
to a function. ( Then access the array elements from thre )

uwe


bria...@gmail.com

unread,
Aug 22, 2012, 12:30:09 PM8/22/12
to
> note that you can only hand over the array name
> to a function. ( Then access the array elements from thre )

Thanks, Uwe.

Roger Oberholtzer's March 2004 thread had a good enough explanation of arrays to help me understand how to proceed...

TCL array object creation in C
https://groups.google.com/d/topic/comp.lang.tcl/sl2mYJv4u0g/discussion

From this thread, I understand that the script writer should create an array with the appropriate name-value pairs. Then pass through to the command the name of the array variable. In the C code, I'll receive a Tcl_Object* which is a string name of the array variable. Then I can do various Tcl_GetVar [1] calls to retrieve the name-value pairs out of the array.

If I can upgrade from TCL 8.4 to 8.5+ then I can use a 'dict' object.

[1] Tcl_GetVar doc http://www.tcl.tk/man/tcl8.4/TclLib/SetVar.htm

Brian
0 new messages