INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Determing variable type

Determing variable type

(OP)
Does Tcl have a command that will return a variables type anlogous to 'winfo class' ?

RE: Determing variable type

(OP)
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:

Quote:


    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.

RE: Determing variable 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

# get length of list my_string
% llength $my_string
4

# iterate over the list elements
% foreach list_element $my_string {
   puts $list_element
}
this
is
my
string
%

Here
http://wiki.tcl.tk/440 I found the following quotes:

Quote:


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

RE: Determing variable type

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close