STRUCTURES IN TCL?

0 views
Skip to first unread message

ed

unread,
May 6, 1999, 3:00:00 AM5/6/99
to
Hi all,

Does anyone know if it's possible to create structures in TCL? i.e. a
structure with 2 strings for example?

Thanks,
Ed


Scott Redman

unread,
May 6, 1999, 3:00:00 AM5/6/99
to
You can use named arrays:

set x(foo) hello
set x(bar) world

Or, you can create classes using [incr Tcl] or one
of the other object oriented extensions.

-- Scott

Bryan Oakley

unread,
May 6, 1999, 3:00:00 AM5/6/99
to
ed <nb23...@nortelnetworks.com> wrote in message
news:3731F432...@nortelnetworks.com...

> Hi all,
>
> Does anyone know if it's possible to create structures in TCL? i.e. a
> structure with 2 strings for example?

Use a combination of arrays and/or lists

set clientdb(1,name) "Bryan Oakley"
set clientdb(1,phone) "555-1212"
set clientdb(1,level) "Expert"

set clientdb(2,name) "Ed Somebody"
set clientdb(2,phone) "555-1212"
set clientdb(2,level) "Novice"

or storing them in an unordered list instead of an array:

set clientdb {
{"Bryan Oakley" "555-1212" "Expert"}
{"Ed Somebody" "555-1212" "Novice"}
}

or a combination of the above:

set clientdb(1) {"Bryan Oakley" "555-1212" "Expert"]
set clientdb(2) {"Ed Somebody" "555-1212" "Novice"]

So, while there isn't any built in "structure" data type, you can create
just about any sort of structure you want using arrays and lists.

Sanjay Nayak

unread,
May 6, 1999, 3:00:00 AM5/6/99
to
what should i do if i would like to have a structure with
struct {
int x;
int y;
char;
};

Matt Newman

unread,
May 6, 1999, 3:00:00 AM5/6/99
to
Sanjay Nayak wrote in message <37322F1B...@packetstream.com>...

>Bryan Oakley wrote:
>> Use a combination of arrays and/or lists
>>
>> set clientdb(1,name) "Bryan Oakley"
>> set clientdb(1,phone) "555-1212"
>> set clientdb(1,level) "Expert"
>>
>> set clientdb(2,name) "Ed Somebody"
>> set clientdb(2,phone) "555-1212"
>> set clientdb(2,level) "Novice"
>
>what should i do if i would like to have a structure with
> struct {
> int x;
> int y;
> char;
>};


What Brians examples show are not only different ways of storing
information, but different ways of indexing this data.

If, for example, your "struct" is "struct { int x; int y; char *label; }"
and this is indexed by x,y -> label then the following would be one way:

set store(44,66) hello
etc.

But only if x,y represents a unique constraint in your data, OTOH if it is
really label -> x,y then

set store(hello) {44 66}

might be more appropriate - again if label is a unique index.

If label is not then the following is also a way of using non-unique indexes

set label hello
set x 44
set y 66

lappend store($label) $x $y

OR

lappend store($label) [list $x $y]

One way of considering use of arrays is as a high-performance indexed table,
where you could have multiple indexes, e.g.

$store($label) -> $x $y ;# UNIQUE PRIMARY INDEX
$store(idx-x,$x) -> list of $label ;# ON-TO INDEX
$store(idx-y,$y) -> list of $label ;# ON-TO INDEX

Or if you have lots of named attributes assoc with a unique primary key:-

set store($key,fld1) $val1
set store($key,fld2) $val2

Which was of course Brian's first example.

I hope this helps - consider the true nature of the data, and the
relationships between members of the "struct" - I think you will find that
doing this stuff in Tcl is 100 times simpler than C for many situations due
to the inherent flexibility of strings.

Later

Matt Newman


Paul Duffin

unread,
May 7, 1999, 3:00:00 AM5/7/99
to
Sanjay Nayak wrote:
>
> what should i do if i would like to have a structure with
> struct {
> int x;
> int y;
> char;
> };
>

Wait for Feather which is an extension which will provide a
lot of useful stuff, specifically for you

struct define node {
x
y
s
}

set a [struct create node {1 2 "hello"}]
getx $a x
setx $a x 12

Structures do not have types or initial values associated
with them although I do intend to add the latter and possible
provide a mechanism to allow some form of validation which
would emulate the former.

--
Paul Duffin
DT/6000 Development Email: pdu...@hursley.ibm.com
IBM UK Laboratories Ltd., Hursley Park nr. Winchester
Internal: 7-246880 International: +44 1962-816880

ed

unread,
May 7, 1999, 3:00:00 AM5/7/99
to
thanks for the structure help, you're awesome!

Bryan Oakley wrote:

> ed <nb23...@nortelnetworks.com> wrote in message
> news:3731F432...@nortelnetworks.com...
> > Hi all,
> >
> > Does anyone know if it's possible to create structures in TCL? i.e. a
> > structure with 2 strings for example?
>

> Use a combination of arrays and/or lists
>
> set clientdb(1,name) "Bryan Oakley"
> set clientdb(1,phone) "555-1212"
> set clientdb(1,level) "Expert"
>
> set clientdb(2,name) "Ed Somebody"
> set clientdb(2,phone) "555-1212"
> set clientdb(2,level) "Novice"
>

Bryan Oakley

unread,
May 7, 1999, 3:00:00 AM5/7/99
to
Sanjay Nayak <san...@packetstream.com> wrote in message
news:37322F1B...@packetstream.com...

> what should i do if i would like to have a structure with
> struct {
> int x;
> int y;
> char;
> };

Well, for one thing, tcl doesn't have a concept of ints and chars.
Everything is a string. But you could easily do something like this:

proc mystruct {intx inty char} {
return [list $intx $inty $char]
}
....
set foo [mystruct 10 20 x]
....
set char [lindex $foo 2]
set intx [lindex $foo 0
set inty [lindex $foo 1]

There are other variations as well. This, for example, uses arrays:

proc mystruct {varname intx inty char} {
upvar $varname struct
set struct(intx) $intx
set struct(inty) $inty
set struct(char) $char
}
....
mystruct foo 10 20 x
.....
set intx $foo(intx)
set inty $foo(inty)
set char $foo(char)


James Ingham

unread,
May 7, 1999, 3:00:00 AM5/7/99
to
ed <nb23...@nortelnetworks.com> writes:

> Hi all,
>
> Does anyone know if it's possible to create structures in TCL? i.e. a
> structure with 2 strings for example?
>

TclX has the "keyed list" data type which is very handy for this kind
of thing. It makes it very easy to define structures that contain
structures, etc...

Jim

--
++==++==++==++==++==++==++==++==++==++==++==++==++==++==++==++==++==++==++
Jim Ingham jin...@cygnus.com
Cygnus Solutions Inc.

lvi...@cas.org

unread,
May 8, 1999, 3:00:00 AM5/8/99
to

According to ed <nb23...@nortelnetworks.com>:
:Does anyone know if it's possible to create structures in TCL? i.e. a

:structure with 2 strings for example?

yes, several people know.

From <URL: http://www.purl.org/NET/Tcl-FAQ/part5.html>, here are a
few of the alternatives already coded ...

What: binary data access - tclbin (Demailly)
Where: <URL: http://www.box.eu.org/%7Edl/tclbin.html>
<URL: ftp://ftp.box.eu.org/tcl/tclbin.tar.gz>
<URL: ftp://ftp.neosoft.com/languages/tcl/sorted/packages-7.6/devel/tclbin-1.2.tar.gz>
Description: TclBin allows access to binary data from Tcl, including a
paradigm for pointers, structures, etc. Latest version
can be built as a Tcl 7.6 dynamically loadable extension.
A sample geturl script is included. Send a
"subscribe tclbin Your Name" line to
<URL: mailto:list...@mail.box.eu.org> to subscribe to the tclbin
mailing list.
While this version compiles and passes all tests under Tcl 8,
it doesn't take advantage of the new Tcl objects.
Updated: 10/1998
Contact: <URL: mailto:L...@demailly.com> (Laurent Demailly)
<URL: mailto:list...@mail.box.eu.org> (Tclbin mailing list)

What: Feature
Where: From the contact
Description: Contact has begun writing a set of mutable Tcl object types
which will eventually include map/array, vector/list, string,
structure, chain/linked list as well as generic methods for accessing
these types. Also considerations regarding lamdba functions,
curried functions, etc.
Update: 03/1999
Contact: <URL: mailto:pdu...@hursley.ibm.com> (Paul Duffin)

What: STERNO
Where: <URL: ftp://ftp.neosoft.com/languages/tcl/TclX/sterno0.3.tar.gz>
Description: STERNO - Simple Tcl Extra Really Nice Objects - is a simple
object system for Tcl implemented in Tcl. It is designed for
structured data encapsulation and management. It is not intended
to replace itcl - for instance, there is currently no inheritance.
Built on top of Tcl 8.0 namespace, it was developed because it
was needed by the contact.
Updated: 10/1998
Contact: <URL: mailto:ma...@grizzly.com> (Mark Diekhans)

What: structure-like objects in Tcl (Burdick)
Where: From the contact
Description: A Tcl command that lets you use arrays similarly to structs.
Updated:
Contact: <URL: mailto:bur...@ars.rtp.nc.us> (Bill Burdick)

What: structure-like objects in Tcl (Gerdes)
Where: From the contact
Description: Set of Tcl procedures to pass structures by reference.
Updated:
Contact: <URL: mailto:dpge...@zorro.cecer.army.mil> (David Gerdes)

What: TclObjectCommand
Where: <URL: http://ftp.austintx.net/users/jatucker/TclObjectCommand/Default.htm>
<URL: http://ftp.austintx.net/users/jatucker/TclObjectCommand/faq.htm>
Description: Library to define Tcl commands which manipulate C++ class
and structure objects in manners similar to the way Tk manages
widgets. Objects can then be manipulated from either C++ or Tcl.
Originally developed to work against code generated by CORBA idl
output.
Updated: 08/1998
Contact: <URL: mailto:jatu...@austin.dsccc.com> ???

What: tclStruct
Where: <URL: ftp://ftp.neosoft.com/languages/tcl/sorted/packages-7.6/devel/tclStruct1.3.tar.gz>
Description: Tcl 7.4 and Tcl 7.5 extension for accessing complex data
structures.
Updated: 10/1998
Contact: <URL: mailto:Matthew....@SanDiegoCA.NCR.com>

--
<URL: mailto:lvi...@cas.org> Quote: Saving the world before bedtime.
<*> O- <URL: http://www.purl.org/NET/lvirden/>
Unless explicitly stated to the contrary, nothing in this posting
should be construed as representing my employer's opinions.

Bruce S. O. Adams

unread,
May 9, 1999, 3:00:00 AM5/9/99
to

lvi...@cas.org wrote:

caught a deja-vu bot in an error :-). I'm pretty sure the extension referenced
above is called Feather not feature.

Reply all
Reply to author
Forward
0 new messages