There is no operation to delete / delete a variable. The only time a variable becomes unset again is at the end of a function call when its unused function argument is used as a local variable:
$ cat tst.awk function foo( arg ) { if ( (arg=="") && (arg==0) ) { print "arg is not set" } else { printf "before assignment: arg=<%s>\n",arg } arg = rand() printf "after assignment: arg=<%s>\n",arg print "----" } BEGIN { foo() foo() } $ awk -f tst.awk file arg is not set after assignment: arg=<0.237788> ---- arg is not set after assignment: arg=<0.291066> ----
therefore, if you want to perform some actions of A, then reset the variable X and then perform actions B, you can encapsulate A and / or B in the function using X as a local variable.
Note that the default value is zero or zero, not zero or false, because its type is "numeric string".
You check for an undefined variable by comparing it with zero and zero:
$ awk 'BEGIN{ if ((x=="") && (x==0)) print "y" }' y $ awk 'BEGIN{ x=0; if ((x=="") && (x==0)) print "y" }' $ awk 'BEGIN{ x=""; if ((x=="") && (x==0)) print "y" }'
If you NEED to have a variable that you delete, you can always use a singleton array:
$ awk 'BEGIN{ if ((x[1]=="") && (x[1]==0)) print "y" }' y $ awk 'BEGIN{ x[1]=""; if ((x[1]=="") && (x[1]==0)) print "y" }' $ awk 'BEGIN{ x[1]=""; delete x; if ((x[1]=="") && (x[1]==0)) print "y" }' y
but IMHO what confuses your code.
What would be the use case to reset a variable? What would you do with this that you cannot do with var="" or var=0 ?
source share