Google 網路論壇不再支援新的 Usenet 貼文或訂閱項目,但過往內容仍可供查看。

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

瀏覽次數:56 次
跳到第一則未讀訊息

Juge

未讀,
2017年10月21日 上午10:08:312017/10/21
收件者:
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

未讀,
2017年10月21日 中午12:52:062017/10/21
收件者:
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

未讀,
2017年10月23日 下午5:53:402017/10/23
收件者:

>
> 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

未讀,
2017年10月24日 凌晨4:18:322017/10/24
收件者:
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

未讀,
2017年10月24日 清晨6:16:072017/10/24
收件者:
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 則新訊息