Can structures (like "struct" in C) be defined in tcl-scripting, or ?

74 views
Skip to first unread message

Jens Ulrik Pedersen

unread,
Feb 13, 2002, 9:36:26 AM2/13/02
to
Hi All.

I need at little help on this matter, im a "newbie" on TCL, and i would like
to know, if a struct can be made in Tcl-scripts ?.
And if structs can be made (or something which replaces structs), can a
pointer be defined to this struct also in Tcl scripting ?

Thx...Jens

Chris Nelson

unread,
Feb 13, 2002, 11:45:45 AM2/13/02
to

Well, yes and no. I've heard of extensions that allow you to do
struct-like things in Tcl but maybe you should tell us what you really
want to accomplish and we can suggest a more Tcl-ish way to accomplish
it. For example, whereas I might write C like:

struct s {
int foo
char bar
float grill
}

s.foo = 1;
s.bar = 'c';
s.grill = 3.14159;

(Bad pseudocode, of course, but you get the idea.) I'd write Tcl like:

set someArray(s,foo) 1
set someArray(s,bar) c
set someArray(s,grill) 3.14159

Does that help any?

Chris
--
The only thing necessary for the triumph of evil
is for good men to do nothing. -- Edmund Burke

Bob Techentin

unread,
Feb 13, 2002, 12:01:15 PM2/13/02
to
"Chris Nelson" <ch...@pinebush.com> wrote in message
news:3C6A9839...@pinebush.com...

> Jens Ulrik Pedersen wrote:
> >
> > I need at little help on this matter, im a "newbie" on TCL, and i
would like
> > to know, if a struct can be made in Tcl-scripts ?.
> > And if structs can be made (or something which replaces structs),
can a
> > pointer be defined to this struct also in Tcl scripting ?
>
> Well, yes and no. I've heard of extensions that allow you to do
> struct-like things in Tcl but maybe you should tell us what you really
> want to accomplish and we can suggest a more Tcl-ish way to accomplish
> it. For example, whereas I might write C like:
>
> struct s {
> int foo
> char bar
> float grill
> }
>
> s.foo = 1;
> s.bar = 'c';
> s.grill = 3.14159;
>
> (Bad pseudocode, of course, but you get the idea.) I'd write Tcl
like:
>
> set someArray(s,foo) 1
> set someArray(s,bar) c
> set someArray(s,grill) 3.14159

Good example, Chris.

Another alternative is to use one of the Object Oriented extensions to
Tcl. My personal favorite is Incr Tcl (which comes with TclPro and
ActiveTcl), but Stooop is available in Tcllib (http://tcllib.sf.net/)
which is particularly easy to install yourself. You could define
structures (classes without any methods) something like this:

itcl::class MyStruct {
public variable foo
public variable bar
public variable grill
}
MyStruct s
s configure -foo 1 -bar c -grill 3.14159

Bob
--
Bob Techentin techenti...@mayo.edu
Mayo Foundation (507) 538-5495
200 First St. SW FAX (507) 284-9171
Rochester MN, 55901 USA http://www.mayo.edu/sppdg/

Phil Ehrens

unread,
Feb 13, 2002, 2:20:12 PM2/13/02
to
Darn easy.

Chris Nelson wrote:
> For example, whereas I might write C like:
>
> struct s {
> int foo
> char bar
> float grill
> }
>
> s.foo = 1;
> s.bar = 'c';
> s.grill = 3.14159;

proc struct { name thingys } {
foreach thing $thingys {
uplevel set $name.$thing \[ list \]
}
}

struct s {
foo
bar
grill
}

set s.foo 1
set s.bar c
set s.grill 3.14159

Of course, you probably dont just want to init the "struct"
to nulls, but to reasonable values:

proc struct { name thingys } {
foreach { key value } $thingys {
uplevel set $name.$key $value
}
}

struct s {
foo 1
bar c
grill 3.14159
}

marsd

unread,
Feb 13, 2002, 6:25:29 PM2/13/02
to
peh...@nospam.ligo.caltech.edu (Phil Ehrens) wrote in message news:<a4ee9c$m...@gap.cco.caltech.edu>...


That is a neat functional example. Thank You :)

I assume you have worked through the variations
with linked lists, and arrays as members?
Are there any drawbacks to these? Besides the
problems with newbies like me getting lost in the
levels...

TIA.

Cameron Laird

unread,
Feb 13, 2002, 8:15:38 PM2/13/02
to
In article <6358834.02021...@posting.google.com>,

marsd <nomob...@hotmail.com> wrote:
>peh...@nospam.ligo.caltech.edu (Phil Ehrens) wrote in message news:<a4ee9c$m...@gap.cco.caltech.edu>...
>> Darn easy.
.
.
.

>> proc struct { name thingys } {
>> foreach thing $thingys {
>> uplevel set $name.$thing \[ list \]
>> }
>> }
.
.
.

>That is a neat functional example. Thank You :)
>
>I assume you have worked through the variations
>with linked lists, and arrays as members?
>Are there any drawbacks to these? Besides the
>problems with newbies like me getting lost in the
>levels...
>
>TIA.

I, also, salute Phil's example.

At a high level, my response to this question is that
Tcl doesn't need 'em--linked lists, and arrays as
members, and such. While my claim is untrue, it's
not *very* untrue. Good style in Tcl is different
from good style in C, in several regards, and Tcl has
different means for achieving the ends that linked
lists (for example) achieve in C.

So: we can go in a couple of directions. Richard
Suchenwirth is particularly active in publishing
example code of such academic abstractions as linked
lists, and perhaps he or one of the others of us will
do so in the cases about which you ask. Or you might
choose to explain that there happens to be a more
fundamental motivation for you, and then we can exa-
mine the appropriate Tcl idioms for solving *that*
problem.
--

Cameron Laird <Cam...@Lairds.com>
Business: http://www.Phaseit.net
Personal: http://starbase.neosoft.com/~claird/home.html

Arjen Markus

unread,
Feb 14, 2002, 3:15:37 AM2/14/02
to

I use a simple convention for this type of things - and if you have
access
to the wonderful book "Structure and Interpretation of Computer
Programs"
you will see that it is a common technique - tagged lists.

For example:
- {POINT 1.0 2.0 3.0} could represent a point in 3D space.
- {LINE {POINT 1.0 2.0 3.0} {VECTOR 1.0 0.0 0.4}} could represent a line
in 3D space.
- etc.

Check out: <http://mini.net/tcl/2831.html>

Regards,

Arjen

marsd

unread,
Feb 14, 2002, 6:19:34 AM2/14/02
to
cla...@starbase.neosoft.com (Cameron Laird) wrote in message news:<F39215872F077BAA.D01B1F09...@lp.airnews.net>...

Mr.Laird,
No problem , my interest was almost purely theoretical.
I agree also that tcl, has other ways to do things.
Just interested in seeing an example of these constructs
and how they operate in tcl. Since , with this example
as a beginning, they seem at least as intuitive as in C.

The almost caveat comes in response to a baby game engine
project in tcl where the use of tcl native arrays and lists
is profuse. The idea popped into my head: "is this another
weapon in the arsenal, and how far can I go with it.."
Thanks,
MMD

Kurt Sierens

unread,
Feb 14, 2002, 6:51:59 AM2/14/02
to
"Jens Ulrik Pedersen" <JPM...@hotmail.com> wrote in message
news:a4dtpv$3cg$1...@news.mch.sbs.de...

I often use namespaces for this purpose

namespace eval ::entity:: {
variable id -1;
variable name "";
variable length 0.0;
}

set entity::id 32;
set entity::name "front";
set entity::length 32.546;


Arjen Markus

unread,
Feb 14, 2002, 7:10:40 AM2/14/02
to
marsd wrote:
>

I think this is a very worthwhile subject for a Wiki-page!
There does not seem to be one devoted to "data structures".
But correct me, if I am wrong.

Regards,

Arjen

Phil Ehrens

unread,
Feb 14, 2002, 11:36:05 AM2/14/02
to
Boy, this is weird... I am not getting half of comp.lang.tcl
through the news server here...

Anyway, as far as "worked through variations"... NO.
The question was, I thought, whether you could use
the C struct idiom in Tcl, and the answer was yes.

Based on my experience there is no idiom in any language
which cannot be look-and-feeled pretty nearly in Tcl.

"There are no drawbacks to anything until performance
issues arise"
- Hassan I Sabbah

marsd wrote:
>> .
>> >> proc struct { name thingys } {
>> >> foreach thing $thingys {
>> >> uplevel set $name.$thing \[ list \]
>> >> }
>> >> }
>> .

David N. Welton

unread,
Feb 14, 2002, 11:54:22 AM2/14/02
to
Arjen Markus <Arjen....@wldelft.nl> writes:

> I think this is a very worthwhile subject for a Wiki-page!
> There does not seem to be one devoted to "data structures".
> But correct me, if I am wrong.

"Real" data structures are harder to write, and more complex than
their equivalents in other languages, because of the lack of
references. In my opinion, at least.

--
David N. Welton
Consulting: http://www.dedasys.com/
Personal: http://www.dedasys.com/davidw/
Free Software: http://www.dedasys.com/freesoftware/
Apache Tcl: http://tcl.apache.org/

Don Porter

unread,
Feb 14, 2002, 12:14:15 PM2/14/02
to
David N. Welton wrote:
> "Real" data structures are harder to write, and more complex than
> their equivalents in other languages, because of the lack of
> references. In my opinion, at least.

Yes, when RMS complains about no "linked lists" in Tcl, I think this
is the real point. No references (pointers). Everything is by
value. Closest thing to "by reference" is "by name".

Contrast with Python where everything is a reference.

--
| Don Porter Mathematical and Computational Sciences Division |
| donald...@nist.gov Information Technology Laboratory |
| http://math.nist.gov/~DPorter/ NIST |
|______________________________________________________________________|

Phil Ehrens

unread,
Feb 14, 2002, 12:33:20 PM2/14/02
to
Well, since you can have a global array with the same name as
the proc that represents the structure... I guess "hard" and
"complex" are liable to strong subjective bias. As is "Real".
More of a problem is, I think, the lack of a native typing
mechanism. Text parsing for type is awfully expensive.

Lack of references!?! What the heck is uplevel for then?!?

if { [ uplevel info exists $var ] } {
uplevel this
uplevel that
}

Hmmm... I guess it is kinda sloppy. And I guess it would
be "hard" to make it work cleanly... complex even.
Okay Dave, you win.
Guess I owe you a can of asafoetida, or whatever it was
you said...

David N. Welton

unread,
Feb 14, 2002, 12:55:29 PM2/14/02
to
peh...@nospam.ligo.caltech.edu (Phil Ehrens) writes:

> Well, since you can have a global array with the same name as
> the proc that represents the structure... I guess "hard" and
> "complex" are liable to strong subjective bias. As is "Real".
> More of a problem is, I think, the lack of a native typing
> mechanism. Text parsing for type is awfully expensive.

> Lack of references!?! What the heck is uplevel for then?!?

> if { [ uplevel info exists $var ] } {
> uplevel this
> uplevel that
> }

> Hmmm... I guess it is kinda sloppy. And I guess it would
> be "hard" to make it work cleanly... complex even.
> Okay Dave, you win.

Here's a concrete example, for interested observers, written in
Scheme:

> (define a (list 1 2))
> (define b (list 3 4))
> (define l1 (list a b))
> l1
((1 2) (3 4))
> (set-car! b 30)
> l1
((1 2) (30 4))

In Tcl, more than linked lists, the problem is more complex things,
like trees. In my opinion, of course. And of course it's still
possible to create them. See Andreas' implementations in tcllib's
struct module, which are very useful.

> Guess I owe you a can of asafoetida, or whatever it was
> you said...

I need burritos. They are impossible to find in Italy.

> David N. Welton wrote:
> > Arjen Markus <Arjen....@wldelft.nl> writes:

> >> I think this is a very worthwhile subject for a Wiki-page!
> >> There does not seem to be one devoted to "data structures".
> >> But correct me, if I am wrong.

> > "Real" data structures are harder to write, and more complex than
> > their equivalents in other languages, because of the lack of
> > references. In my opinion, at least.

--

Phil Ehrens

unread,
Feb 14, 2002, 1:25:20 PM2/14/02
to
David N. Welton wrote:
>
>> Guess I owe you a can of asafoetida, or whatever it was
>> you said...
>
> I need burritos. They are impossible to find in Italy.

Ew. Those canned burritos. But you can get the makings:

http://search.store.yahoo.com/cgi-bin/nsearch?follow-pro=1&vwcatalog
=tayabas&vwnitems=0&vwtotal=0.00&basket=5C80e188d8011bb13c6c00b34944
821e188d82273d783200b13479a8c6c8ca2b4&et=3c6d8753&vwentropy=10137110
27&query=burritos&unique=c00b3&catalog=tayabas&et=3c6c07bb&basket=5C
80e188d8011bb13c6c00b34944821e188d82273d783200b13479a8c6c8ca2b4

Jeffrey Hobbs

unread,
Feb 14, 2002, 1:26:37 PM2/14/02
to
Don Porter wrote:
>
> David N. Welton wrote:
> > "Real" data structures are harder to write, and more complex than
> > their equivalents in other languages, because of the lack of
> > references. In my opinion, at least.
>
> Yes, when RMS complains about no "linked lists" in Tcl, I think this
> is the real point. No references (pointers). Everything is by
> value. Closest thing to "by reference" is "by name".

But RMS' manical rants are about older versions of Tcl. Tcl 8+
really does pass most things around by pointer internally.

set a 50
set b $a

In Tcl, b will just incr the ref count to the same object pointed
to by $a. There is magic under all that simplicity.

--
Jeff Hobbs The Tcl Guy
Senior Developer http://www.ActiveState.com/
Tcl Support and Productivity Solutions

David N. Welton

unread,
Feb 14, 2002, 1:29:50 PM2/14/02
to

David N. Welton

unread,
Feb 14, 2002, 1:34:24 PM2/14/02
to
Jeffrey Hobbs <Je...@ActiveState.com> writes:

> But RMS' manical rants are about older versions of Tcl. Tcl 8+
> really does pass most things around by pointer internally.

> set a 50
> set b $a

> In Tcl, b will just incr the ref count to the same object pointed to
> by $a. There is magic under all that simplicity.

Ok, but how would you replicate, in Tcl, the scheme code I posted?
They may be references at the C level, but if everything at the Tcl
level is strings, it doesn't do us much good.

I created some code that actually implements car, cdr and cons at the
Tcl_Obj level, but apparently this has some problems, according to
Mr. Fellows (Message-Id: <3C4BE829...@cs.man.ac.uk>).

Don Porter

unread,
Feb 14, 2002, 1:40:02 PM2/14/02
to
Don Porter wrote:
>> ....no "linked lists" in Tcl...

>> No references (pointers). Everything is by
>> value. Closest thing to "by reference" is "by name".

Jeffrey Hobbs wrote:
> Tcl 8+ really does pass most things around by pointer internally.
>
> set a 50
> set b $a
>
> In Tcl, b will just incr the ref count to the same object pointed
> to by $a. There is magic under all that simplicity.

True, but that's really a matter of internal caching for performance
and memory savings. It doesn't provide "real" references available
at the script level. The "everything is[*] a string" philosophy of
Tcl runs deep.

[*] More accurately, "everything can be a string" these days.

Phil Ehrens

unread,
Feb 14, 2002, 2:26:22 PM2/14/02
to
Right up there with the "Atlas" pasta maker we got as a wedding
gift. You had to be Hercules to use it. Broke a chunk right
off of the kitchen counter the first time I managed to crank
it over. Functionally identical to a paper shredder.

And the gingerbread house kit. What were they thinking with
that. No way anybody could have got those walls to stay up.
As soon as you put the roof on the walls collapsed.

marsd

unread,
Feb 14, 2002, 11:19:37 PM2/14/02
to
peh...@nospam.ligo.caltech.edu (Phil Ehrens) wrote in message news:<a4gp1l$o...@gap.cco.caltech.edu>...

> Based on my experience there is no idiom in any language
> which cannot be look-and-feeled pretty nearly in Tcl.

Yeah, I've noticed that to some large degree.

>
> "There are no drawbacks to anything until performance
> issues arise"
> - Hassan I Sabbah

Sounds like my girlfriend.

Thanks.

Arjen Markus

unread,
Feb 15, 2002, 2:53:48 AM2/15/02
to
"David N. Welton" wrote:
>

>
> Ok, but how would you replicate, in Tcl, the scheme code I posted?
> They may be references at the C level, but if everything at the Tcl
> level is strings, it doesn't do us much good.
>
> I created some code that actually implements car, cdr and cons at the
> Tcl_Obj level, but apparently this has some problems, according to
> Mr. Fellows (Message-Id: <3C4BE829...@cs.man.ac.uk>).
>

I have set up a first draft of a Wiki page for discussing the
merits and problems of the various styles of managing data structures.
(Note the word "draft"):

<http://mini.net/tcl/2995.html>

Feel free to expand it, correct it etc. (I think the topic is worth our
while!)

Regards,

Arjen

lvi...@yahoo.com

unread,
Feb 15, 2002, 7:59:09 AM2/15/02
to

According to Cameron Laird <cla...@starbase.neosoft.com>:
: and then we can exa-

:mine the appropriate Tcl idioms for solving *that*
:problem.


And hopefully accumulate these mappings of tcl idioms <-> common algorithm
solutions into wiki pages...

--
"I know of vanishingly few people ... who choose to use ksh." "I'm a minority!"
<URL: mailto:lvi...@cas.org> <URL: http://www.purl.org/NET/lvirden/>
Even if explicitly stated to the contrary, nothing in this posting
should be construed as representing my employer's opinions.

Iain B. Findleton

unread,
Feb 15, 2002, 7:05:10 AM2/15/02
to
Kurt Sierens wrote:
>
> "Jens Ulrik Pedersen" <JPM...@hotmail.com> wrote in message
> news:a4dtpv$3cg$1...@news.mch.sbs.de...
> > Hi All.
> >
> > I need at little help on this matter, im a "newbie" on TCL, and i would
> like
> > to know, if a struct can be made in Tcl-scripts ?.
> > And if structs can be made (or something which replaces structs), can a
> > pointer be defined to this struct also in Tcl scripting ?
> >
> > Thx...Jens
> >
> >
> >

You could go to my site, grab the Containers package, then do things
like:

package require Containers

# Create a structure template

set person [struct Name Address Phone]

# Create instances of the structure

set a [$person Iain Montreal 457-0744]
set b [$person Bob Tampa 555-3017]
set c [$person Carol Chicago 555-8821]
set d [$person Jeff Vancouver 555-0007]

# Print out the elements of a structure

puts "Elements [$b -elements]"

# Implement a name change for Bob, presumably because of a
# transexual experience...

$b Name Alice

# and Carol has moved to Montreal because it is a center of Tcl
# excellence...

$c Address Montreal Phone 555-7035

# Here is a handy feature for those who, like me, forget the
# spelling of their namespace names. A struct instance can be
# passed as a parameter to a proc...

proc Subroutine { s } {

global Data

# Strictly speaking, this is not needed, as the package does
# this for all containers using variable trace. However...

foreach { name value } [$s -list] {
set Data($name) $value
}
}

# Make the data for Jeff ubiquitous...

Subroutine $d

# Now check that Jeff is indeed everywhere...

foreach item [array names Data] {
puts "$item $Data($item)"
"

and so on. The documentation for the package has examples of how to
manipulate structures and their elements in ways reminiscent of C++, my
second favourite language.

--
Iain B. Findleton
http://pages.infinit.net/cclients
custom...@videotron.ca
(514)457-0744

Reply all
Reply to author
Forward
0 new messages