Need an example to create lists in Tcl 8.0 using Object 
Author Message
 Need an example to create lists in Tcl 8.0 using Object

Hi,

I need to write a C function using Tcl 8.0 library.
In this function I need to create 3 lists containing strings, and then
sending as the result a list containg these 3 lists.
I can create the 3 lists of strings using Tcl_Merge but then I have a
problem: how can I create the result list containg these 3 lists.

I'd also like to use the new object API, because it may be faster to
execute, but the manual pages don't have any example and the Tcl
source code isn't of any help about this.

Thanks  



Sun, 12 Mar 2000 03:00:00 GMT  
 Need an example to create lists in Tcl 8.0 using Object



Quote:
> I need to write a C function using Tcl 8.0 library.
> In this function I need to create 3 lists containing strings, and then
> sending as the result a list containg these 3 lists.
> I can create the 3 lists of strings using Tcl_Merge but then I have a
> problem: how can I create the result list containg these 3 lists.

Use Tcl_Merge() again.  A list is just a specially formatted string,
and the tcl library is happy to look after that for you.  Note that
what I say here is the way it is pre 8.0.  Then things get a tiny
little bit different.

Quote:
> I'd also like to use the new object API, because it may be faster to
> execute, but the manual pages don't have any example and the Tcl
> source code isn't of any help about this.

Try looking at this function:

   Tcl_Obj *
   make_nested_list(argc1, argv1,  argc2, argv2,  argc3, argv3)
        int argc1, argc2, argc3;
        char *argv1[], *argv2[], **argv3[];
   {
       Tcl_Obj *vec1[3], **vec2;
       int i, maxargc;

       /* Get the maximum argument count... */
       if (argc1>argc2) {
           if (argc1>argc3)
               maxargc = argc1;
           else
               maxargc = argc3;
       } else if (argc2>argc3) {
           maxargc = argc2;
       } else {
           maxargc = argc3;
       }
       /* ...and allocate an array of that size */
       vec2 = (Tcl_Obj **)Tcl_Alloc(sizeof(Tcl_Obj *)*maxargc);

       /* Make the first list */
       for (i=0 ; i<argc1 ; i++) {
           vec2[i] = Tcl_NewStringObj(argv1[i], -1);
       }
       vec1[0] = Tcl_NewListObj(argc1, vec2);

       /* Make the second list */
       for (i=0 ; i<argc2 ; i++) {
           vec2[i] = Tcl_NewStringObj(argv2[i], -1);
       }
       vec1[1] = Tcl_NewListObj(argc2, vec2);

       /* Make the third list */
       for (i=0 ; i<argc3 ; i++) {
           vec2[i] = Tcl_NewStringObj(argv3[i], -1);
       }
       vec1[2] = Tcl_NewListObj(argc3, vec2);

       /* Get rid of the array we allocated */
       Tcl_Free((char *)vec2);

       /* Return the overall list */
       return Tcl_NewListObj(3, vec1);
   }

Does that make things any clearer?

Donal.
--
Donal K. Fellows   http://r8h.cs.man.ac.uk:8000/    Boing!  Boing!  Boing!

--
The at symbol in my email address may have been shifted one word right
to deter spam-bots.  Sorry for any inconvenience this causes you.
--
I don't buy from or respond to spammers.  Ever.  Do you?



Tue, 21 Mar 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Creating lists in Tcl 8.0 using Object

2. need example of tcl dll created with vc++

3. passing a list of created objects to a tcl script

4. Need example of working tcl/tk script used as cgi-bin script

5. Tcl 8.0 BUG when creating invalid line in canvas

6. Accessing Global variables/objects from C code in Tcl 8.0

7. Is this a bug - list handling in TCL 8.0

8. Object-orientation for Tcl/Tk 8.0?

9. TclBin 1.2 {full binary objects structures and IO for Tcl 7.3 to 8.0}

10. Porting to the Tcl 8.0 object interface

11. custom Tk_Main using Tcl/Tk 8.0

12. Problems with HP-UX 8.0 and Tcl/TK 8.0

 

 
Powered by phpBB® Forum Software