Note that there are some explanatory texts on larger screens.

plurals
  1. POEnumerate or list all variables in a program of [your favorite language here]
    primarykey
    data
    text
    <p>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:</p> <pre> #!/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 </pre> <p>which will output something like:</p> <pre> __builtins__ is &lt;type 'str'&gt; and is equal to &lt;module '__builtin__' (built-in)&gt; __doc__ is &lt;type 'str'&gt; and is equal to None __file__ is &lt;type 'str'&gt; and is equal to ./foo.py __name__ is &lt;type 'str'&gt; and is equal to __main__ foo1 is &lt;type 'str'&gt; and is equal to Hello world foo2 is &lt;type 'str'&gt; and is equal to bar foo3 is &lt;type 'str'&gt; and is equal to {'1': 'a', '2': 'b'} foo4 is &lt;type 'str'&gt; and is equal to 1+1 </pre> <p>I have so far found a partial way in PHP (courtesy of <a href="http://www.phpro.org/examples/List-all-variables.html" rel="noreferrer">link text</a>) but it only lists all variables and their types, not the contents:</p> <pre> &lt;?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-&gt;getIterator(); $iterator-&gt;valid(); $iterator-&gt;next()) { echo $iterator-&gt;key() . ' =&gt; ' . $iterator-&gt;current() . '&lt;br /&gt;'; } ?&gt; </pre> <p><strong>So I put it to you: how do you list all variables and their contents in your favorite language?</strong></p> <hr> <p>Edit by <a href="https://stackoverflow.com/users/6309/vonc">VonC</a>: I propose this question follows the spirit of a little "<a href="https://stackoverflow.com/questions/172184">code-challenge</a>".<br> If you do not agree, just edit and remove the tag and the link.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload