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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determing variable type

Status
Not open for further replies.

PinkeyNBrain

IS-IT--Management
Dec 12, 2006
279
US
Does Tcl have a command that will return a variables type anlogous to 'winfo class' ?
 
That is a start. I have a subroutine I'm trying to make more robust. It's opening definition is
Code:
proc parse_info {args} {xxx}
From the 'args' values, I'll know what to do if the argument is a string (a string in the common sence), a list, or an array. Consider the following input / output:
console input console output
set foo {this is a string} this is a string
string is list $foo 1
set foo [list this is a list] this is a list
string is list $foo 1
set foo(a) {this is arr a} can't set "foo(a)": variable isn't array
From the first 2 above, I don't have a good way to distinguish between a string or list. However the error from the last console input tells me that somewhere tcl can test between arrays and non-arrays. The 'winfo class' is handy as I can initialize or reset certain vars based on their class. Similar, I am looking to work with the in coming vars based on their type.
 
It seems, that there is no much difference difference between lists and strings in Tcl.
In fact, we can use list operations on strings, e.g.:
Code:
# create string
% set my_string "this is my    string"
this is my    string

# get length of the string
% string length $my_string
20

[COLOR=blue]# get length of list my_string[/color]
% llength $my_string
4

[COLOR=blue]# iterate over the list elements[/color]
% foreach list_element $my_string {
   puts $list_element
}
this
is
my
string
%

Here
I found the following quotes:
From a more technical perspective, a list is just a string with a particular syntax. Therefore, in Tcl, a string is either a valid list or not.
....
Tcl, as a general language, does not treat a list differently from other strings. This means it is possible to use lists anywhere one can use arbitrary strings
 
Thanks - I was finding similar corrilations between strings and lists through various console tests. The routine I'm building isn't called often and it is not required to run fast. This gives me some leway on how to solve the issue. Right now I'll first treat the incoming var as if it were a list and use a 'catch' statement while doing so. If the catch triggers I'll examine it to see if the parcer know's it's an array, and if so, finish parcing as if it is an array. It's not elegant, but will get done what I need at this point.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top