Home > Archive > AWK > November 2007 > how to list all defined variables
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
how to list all defined variables
|
|
| Eric Pement 2007-10-26, 6:58 pm |
| Is there a way within awk (or within gawk) to list all the variable
names and values currently defined in the script? I am looking for a
function or code block that can go in END{...} of the script. TIA.
| |
| Vassilis 2007-10-26, 6:58 pm |
| On 26 , 19:43, Eric Pement <peme...@northpark.edu> wrote:
> Is there a way within awk (or within gawk) to list all the variable
> names and values currently defined in the script? I am looking for a
> function or code block that can go in END{...} of the script. TIA.
Currently, there is no support for getting any reflective information
from (g)awk. You can simulate it though, with a bit of work. You could
init an array with the names of all your variables and operate on them
accordingly. The drawback is you can't use it as easily on array
variables. This trick I've also used for like-namespace issues.
Vassilis
| |
| Ed Morton 2007-10-26, 6:58 pm |
| Eric Pement wrote:
> Is there a way within awk (or within gawk) to list all the variable
> names and values currently defined in the script? I am looking for a
> function or code block that can go in END{...} of the script. TIA.
>
I've never used it, but GNU awk has a -DDEBUG compilation flag that may
do something like what you want.
Alternatively, write your script as:
...
{ V["var1"] = 3; V["var2"] = 7 }
...
END{ for (var in V) print var "=" V[var] }
or, if you prefer to not have to remember to use quoted strings every
time you use each "variable":
BEGIN{ var1="var1"; var2="var2" }
...
{ V[var1] = 3; V[var2] = 7 }
...
END{ for (var in V) print var "=" V[var] }
Regards,
Ed.
| |
| Manuel Collado 2007-10-26, 6:58 pm |
| Eric Pement escribió:
> Is there a way within awk (or within gawk) to list all the variable
> names and values currently defined in the script? I am looking for a
> function or code block that can go in END{...} of the script. TIA.
>
gawk has a --dump-variables command switch that reports all defined
variables with their final values at program end (i.e., after the END rule).
Hope this helps.
--
Manuel Collado - http://lml.ls.fi.upm.es/~mcollado
| |
| Kenny McCormack 2007-10-26, 6:58 pm |
| In article <472233e5$1@news.upm.es>,
Manuel Collado <m.collado@lml.ls.fi.upm.es> wrote:
>Eric Pement escribió:
>
>gawk has a --dump-variables command switch that reports all defined
>variables with their final values at program end (i.e., after the END rule).
Indeed it does. And it also has "pgawk", which is more execution
oriented than "list variables" oriented. Between the two of them, I
think we've put paid to the assertion that gawk has no reflective
capabilities.
| |
| Vassilis 2007-10-26, 6:58 pm |
| On 26 , 22:31, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
> In article <472233e...@news.upm.es>,
> Manuel Collado <m.coll...@lml.ls.fi.upm.es> wrote:
>
>
le).[color=darkred]
>
> Indeed it does. And it also has "pgawk", which is more execution
> oriented than "list variables" oriented. Between the two of them, I
> think we've put paid to the assertion that gawk has no reflective
> capabilities.
It still lacks *runtime* reflective capabilities, such as found in
Python, Tcl, etc.
Still, the OP might be lucky enough to find a use for --dump-
variables.
Vassilis
| |
| Kees Nuyt 2007-10-26, 6:58 pm |
| On Fri, 26 Oct 2007 09:43:09 -0700, Eric Pement
<pemente@northpark.edu> wrote:
>Is there a way within awk (or within gawk) to list all the variable
>names and values currently defined in the script? I am looking for a
>function or code block that can go in END{...} of the script. TIA.
From the GNU Awk User's Guide :
<citation>
Command-Line Options
[...]
--dump-variables[=file]
Print a sorted list of global variables, their types, and
final values to file. If no file is provided, gawk prints
this list to a file named awkvars.out in the current
directory.
Having a list of all the global variables is a good way to
look for typographical errors in your programs. You would
also use this option if you have a large program with a
lot of functions, and you want to be sure that your
functions don't inadvertently use global variables that
you meant to be local. (This is a particularly easy
mistake to make with simple variable names like i, j, and
so on.)
</citation>
This only outputs the values when the script finishes (and
thus satisfies your END{} requirement, but nobody stops
you to insert an
exit 0
to abort the script at the point of execution you are
interested in.
HTH
--
( Kees
)
c[_] The desire to become a politician should bar you
for life from ever becoming one. (Billy Connolly) (#232)
| |
|
|
|
|
|