GAWK : Turning a variable value into a variable name (121 Views)
Reply
Occasional Visitor
Andrew Jewell
Posts: 1
Registered: ‎04-27-2009
Message 1 of 2 (121 Views)

GAWK : Turning a variable value into a variable name

I need to fill in the middle statement in the following GAWK script:

X = "foo"
# something like @X = 7
assert( foo == 7 )

in perl I'd just use $$X. Is there a way to do this in GAWK?
Acclaimed Contributor
Dennis Handly
Posts: 25,094
Registered: ‎03-06-2006
Message 2 of 2 (121 Views)

Re: GAWK : Turning a variable value into a variable name

I don't know of any. You could use associative arrays:
X = "foo"
mapVar[X] = 7
assert(mapVar["foo"] == 7)

I.e. if you want to do the indirection, you always have to use mapVar[].
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.