Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

identification of 'atoms' and 'lists' in TCL

Status
Not open for further replies.

johnlopez2000

Programmer
Aug 2, 2002
90
US
I am using TCL in a programming environment backended by an Oracle database and a middleware/PDM (engineering Product Data Mgmt) product called Matrix. TCL is the native script language in this environment.

TCL progs make calls to the underlying middleware piece via a command in this specific implementation, i.e.:

set x [mql SOME_QUERY]

The resultant can be formated within the MQL command to return lists. Sometimes the return values can be nested lists. In the event that the resultant is returned to TCL, then passed to 'lower' level procs, the nesting is increased per level on the stack.

From long ago in my LISP programming days, I remember certain functions that I could call to determine whether an element of a list was an 'atom' or a 'list' as one iterated through the content of one level of the list.

So, I wonder if there is some way in TCL to derive the same information, something like an isAtom() or isList() type functionality.

One of my lists may look like:

{a b c {d e} f {g} {h {i j}} k}

iterating through the above, I'd thought about using LLENGTH, but whether the element is:
a

or:
{g}

the return value is one.

So, if you know of a command or technique that will work to accomplish this, preferably usable in a conditional, I would appreciate the wisdom!
 
Basically, Tcl has no way of distinguishing between "lists" and "atoms." Let's take a look at your example list:

[tt]% set val {a b c {d e} f {g} {h {i j}} k}[/tt]

If I now do:

[tt]% lindex $val 3
d e[/tt]

Tcl has no way of knowing if this result is a 2-element list ("d" and "e") or a 3-character string ("d e"). In fact, the result is both, depending on whether you were to give the result to a string-processing command or a list-processing command.

And as you've noticed, even the {} around the "g" doesn't serve to signal to Tcl that what's inside is a list:

[tt]% lindex $val 5
g[/tt]

The only purpose the quotes serve is to delimit a list element.

The reason is the basic Tcl design principle that everything is a string. This simplifies scripting in most cases, but it does have its drawbacks. As you've seen, one of those limitations is that you can't use simple recursive techniques that rely on being able to distinguish between "atom" and "list." Tcl requires that you actually know something about the structure of the list you're processing. - Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
When strings are passed to list commands (llength, lindex...) they are considered as space separed items lists.
There is no way to say: I want a list of pure strings. Or: this string is not a list.
This is coherent with the philosophy of the creator of Tcl: John Ousterhout.
And this is coherent with the wish of the current Tcl community.

I shall add that, after 3 years of Tcl programming, I desagree and wish a way to say: this string is not a list.
<*sigh*>

ulis
 
Gentlemen,

thank you for your considerate reply. It is most appreciated. If I do come up with some technique, I will apply it to this thread.

Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top