A friend asked me last week how to enumerate or list all variables within a program/function/etc. for the purposes of debugging (essentially getting a snapshot of everything so you can see what variables are set to, or if they are set at all). I looked around a bit and found a relatively good way for Python:
#!/usr/bin/python foo1 = "Hello world" foo2 = "bar" foo3 = {"1":"a", "2":"b"} foo4 = "1+1" for name in dir(): myvalue = eval(name) print name, "is", type(name), "and is equal to ", myvalue
which will output something like:
__builtins__ is <type 'str'> and is equal to <module '__builtin__' (built-in)> __doc__ is <type 'str'> and is equal to None __file__ is <type 'str'> and is equal to ./foo.py __name__ is <type 'str'> and is equal to __main__ foo1 is <type 'str'> and is equal to Hello world foo2 is <type 'str'> and is equal to bar foo3 is <type 'str'> and is equal to {'1': 'a', '2': 'b'} foo4 is <type 'str'> and is equal to 1+1
I have so far found a partial way in PHP (courtesy of link text) but it only lists all variables and their types, not the contents:
<?php // create a few variables $bar = 'foo'; $foo ='bar'; // create a new array object $arrayObj = new ArrayObject(get_defined_vars()); // loop over the array object and echo variables and values for($iterator = $arrayObj->getIterator(); $iterator->valid(); $iterator->next()) { echo $iterator->key() . ' => ' . $iterator->current() . '<br />'; } ?>
So I put it to you: how do you list all variables and their contents in your favorite language?
Edit by VonC: I propose this question follows the spirit of a little "code-challenge".
If you do not agree, just edit and remove the tag and the link.
First, I'd simply use a debugger ;-p Visual Studio, for example, has "Locals" and "Watch" windows that will show all the variables etc you want, fully expandable to any level.
In C# you can't really get at method variables very easily (and they many well be removed by the compiler) - but you can access fields etc via reflection:
Common Lisp:
To also show all bound values:
This is a long list, and not particularly useful. I would really use the integrated debugger.
This is what it would look like in Ruby:
which will output
However, didn't you mean to output the type of object the variable references instead of the type used to represent the variable identifier? IOW, the type of
foo3
should beHash
(ordict
) instead ofString
, right? In that case, the code would beand the result is
In Lua the fundamental data structure is the table and even the global environment _G is a table. So, a simple enumeration will do the trick.
In REBOL, all variables live inside a context of type
object!
. There's a global context, and every function has its own implicit local context. You can create new contexts explicitly by creating a newobject!
(or using thecontext
function). This is different from traditional languages because variables (called "words" in REBOL) carry a reference to their context around with them, even when they have left the "scope" in which they were defined.So, the bottom line is that, given a context, we can list the variables it defines. We'll use Ladislav Mecir's
context-words?
function.Now we can list all the words defined in the global context. (There are a lot of them.)
We can also write a function that then lists the variables it defines.
What we can't do in REBOL, as far as I know, is walk up the context tree, although the interpreter seems to be able to do this perfectly well when it decides how to bind words to their contexts. I think this is because the context tree (i.e., scope) may have one "shape" at the time a word is bound but quite another at the time it's evaluated.
In the R language
and to remove all objects from the working memory