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
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
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/
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
}
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
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
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
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;
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
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 \]
>> >> }
>> >> }
>> .
> 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/
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 |
|______________________________________________________________________|
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...
> 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.
--
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
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
Beans are available here, it's other stuff:
that's hard to get. Hrm... tortilla press... damn, there's a sweet
gift idea.
> 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>).
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.
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.
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.
>
> 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
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.
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