Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Unsetting variables in GNU Awk

339 views
Skip to first unread message

Janis Papanagnou

unread,
Jan 17, 2021, 3:31:33 AM1/17/21
to
Scalar [user-]variables are unset by default so that a test
var==0 && var=="" can be used to check that 'unset' state.
Whenever one assigns a value, e.g. var=0 or var="" that test
will further return 'false'. - Is there some way to unset a
variable again?

Janis

Kaz Kylheku

unread,
Jan 17, 2021, 3:41:17 AM1/17/21
to
Yes.

Firstly, if you can substitute an array element for the variable, then
you can delete the containing array.

E.g. here our variable is z[0]:

$ gawk 'BEGIN { z[0] = 1; delete z ; print z[0] == 0 && z[0] == "" }'
1

See what I mean?

Secondly, we can return a scalar to to he unset state by assigning
to it the value of such a scalar.

$ gawk 'BEGIN { a = 1; print a == 0 && a == "";
a = b; print a == 0 && a == "" }'
0
1

a was able to return to the unset state by copying the value of b,
which was never set.

Not vouching for the portability of that without additional research.

--
TXR Programming Language: http://nongnu.org/txr

Janis Papanagnou

unread,
Jan 17, 2021, 5:50:48 AM1/17/21
to
On 17.01.2021 09:41, Kaz Kylheku wrote:
> On 2021-01-17, Janis Papanagnou <janis_pa...@hotmail.com> wrote:
>> Scalar [user-]variables are unset by default so that a test
>> var==0 && var=="" can be used to check that 'unset' state.
>> Whenever one assigns a value, e.g. var=0 or var="" that test
>> will further return 'false'. - Is there some way to unset a
>> variable again?
>
> Yes.
>
> [...]
>
> Secondly, we can return a scalar to to he unset state by assigning
> to it the value of such a scalar.
>
> $ gawk 'BEGIN { a = 1; print a == 0 && a == "";
> a = b; print a == 0 && a == "" }'
> 0
> 1
>
> a was able to return to the unset state by copying the value of b,
> which was never set.

This is a great idea. Thanks!

> Not vouching for the portability of that without additional research.

Janis

0 new messages