Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

struct::tree how do I query if it exists?

56 views
Skip to first unread message

Juge

unread,
Oct 21, 2017, 10:08:31 AM10/21/17
to
I am using struc::tree in my program and I would like to check if it already exists in the namespace. I do not get anything with info exists or something.
The only way I get a hint is with namespace children <namespace_where_the_tree_is>
but I think there must be a more elegant way for finding it perhaps somehow with exists?

Any suggestions?

Rich

unread,
Oct 21, 2017, 12:52:06 PM10/21/17
to
You don't say if you already know the name, but I'll assume since you
want to know "does this tree exist" that you already know the name it
should be using.

In which case, you can use llength on the return value of [info
commands] to determine if the tree name exists:

$ rlwrap tclsh
% package require struct::tree
2.1.2
% set t [struct::tree]
::tree1
% info commands ::tree1
::tree1
% info commands ::tree2

A zero llength would mean "no tree of this name". A non zero would
mean "a tree of this name" exists.

Now, technically, this is "does this command exist", but as the tree is
a command, you'd then know the tree existed (provided you didn't create
a command of the same name that is not a tree).

But I also have a sense that your request is for a different problem,
one you've left unstated. So what is the issue you are really trying
to solve?

olivier...@gmail.com

unread,
Oct 23, 2017, 5:53:40 PM10/23/17
to

>
> Any suggestions?

I simply use this test :

namespace exists mytree

to check existence of a tree "mytree", and the same for the tcllib matrix, and some other objects created in tcllib using the namespace concept.

Olivier.

Juge

unread,
Oct 24, 2017, 4:18:32 AM10/24/17
to
Am Samstag, 21. Oktober 2017 18:52:06 UTC+2 schrieb Rich:
>
> But I also have a sense that your request is for a different problem,
> one you've left unstated. So what is the issue you are really trying
> to solve?

I think your solution is ok, as well as the one below. I'll try them out. My problem is that I am reading stuff into a struct::tree in this or that proc, the information is either read from an external file or generated from another source.
In both cases I want to start from a clean tree. So I could use catch with destroy, but I think it is more elegant to check if tree exists and in that case delete it before creating it again before filling it.

Rich

unread,
Oct 24, 2017, 6:16:07 AM10/24/17
to
Or, when you are done with the current tree, at the end of making
whatever use of it you make, you destroy it.

Then you don't need to check for existance and delete at the top of
your loop, and an error creating the tree anew there means you didn't
properly destroy the existing tree at some exit point.

Then no catch, nor check for presence, is necessary at the top of the
loop.
0 new messages