How to tell whether a name is a var or array? 
Author Message
 How to tell whether a name is a var or array?

I'm writing a couple of helper procs which, given a list of variable
names, will save the values of those variables to a file which can
then be read back in to restore the variables at a later session.
The tcl "info" and "array" commands let me do this easily, with one
odd exception; I can see of no built-in which lets me check to see
if a variable "x" is a plain-jane variable, or an array.

I could of course test for this by attempting an operation which would
cause an error if the variable was one but not the other, and trap the
error, however this strikes me as inelegant to say the least.  Is there
a better solution?

As always, thanks.

Ken McDonald

.



Thu, 29 Jan 1998 03:00:00 GMT  
 How to tell whether a name is a var or array?

Quote:

>The tcl "info" and "array" commands let me do this easily, with one
>odd exception; I can see of no built-in which lets me check to see
>if a variable "x" is a plain-jane variable, or an array.

You can do an 'array names var' command on vars that aren't arrays and
it will only return an empty string.  Thus you can have this proc:

proc dump_var varname {
    global $varname
    if {[set ix [array names $varname]] != ""} {
        foreach i $ix {
            puts "$varname($i) = [set $varname($i)]"
        }
        return
    }
    if [info exists $varname] {
        puts "$varname = [set $varname]"
    }
    ## Reaching here means it was an invalid variable name

Quote:
}

Works too (what a bonus).
--
     Jeffrey Hobbs                           Office: 503/346-3998

                URL: http://www.cs.uoregon.edu/~jhobbs/


Fri, 30 Jan 1998 03:00:00 GMT  
 How to tell whether a name is a var or array?

Quote:

> You can do an 'array names var' command on vars that aren't arrays and
> it will only return an empty string.

Note that this is true as of Tcl 7.4.

Quote:
> proc dump_var varname {
...
> }

Consider this "hidden" (more like hard to reach) array:

        % upvar 0 test(a) x
        % set x(haha) 1
        1
        % array names x
        haha
        % puts $x(haha)
        1
        % array names test
        a
        % puts $test(a)
        can't read "test(a)": variable is array

John



Sat, 31 Jan 1998 03:00:00 GMT  
 How to tell whether a name is a var or array?


|> >The tcl "info" and "array" commands let me do this easily, with one
|> >odd exception; I can see of no built-in which lets me check to see
|> >if a variable "x" is a plain-jane variable, or an array.
|>
|> You can do an 'array names var' command on vars that aren't arrays and
|> it will only return an empty string.  Thus you can have this proc:
[snip]
|> Works too (what a bonus).

Close, but no cigar.  Try this:

        set foo(bar) xxx
        unset foo(bar)
        set doobry xxx

and then

        parray foo
        set doobry

both work and

        set foo
        parray doobry

both fail.  In fact, the test you're looking for is "array exists ..." and

        array exists foo
        array exists doobry

return 1 and 0 respectively.  Take a look at parray ("info body parray"
after you've invoked it).

--

ISODE Consortium                                        +44 181 332 9091

These are my opinions and have nothing to do with my employer.



Sun, 01 Feb 1998 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. class func & var name overlap

2. Var names in SICStus 2.1

3. [array names -inorder name]

4. array name ARRAY regexp

5. how to access arrays with variable for array names and index

6. access to array if name of array is variable

7. HOWTO: reference elements of an array if array name is in a variable

8. Basic array from shell var

9. Question about array var. in radiobutton

10. 8.0a2 and array set var list

11. awk: -v Var=VAR Obviously not a syntax issue

12. update Var if Var==nil

 

 
Powered by phpBB® Forum Software