help-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Hi there, a question about typeof()


From: Neil R. Ormos
Subject: Re: Hi there, a question about typeof()
Date: Sun, 26 Sep 2021 21:10:07 -0500 (CDT)

david kerns wrote:
> Ivo Palli wrote:

>> |  I ran into this:

>> |  BEGIN {
>> |  split("123", b, "");  # Split a line
>> |  print length(b);      # 3 - as it should
>> |  print typeof(b[0]);   # unassigned
>> |  print typeof(b[1]);   # strnum
>> |  print typeof(b[2]);   # strnum
>> |  print typeof(b[3]);   # strnum
>> |  print typeof(b[4]);   # unassigned
>> |  print length(b);      # 5 - euhm, what?
>> |  # Like quantum physics, observing changes the observed
>> |  exit
>> |  }

> if you add two more print statements to check
> the length of b, you see exactly what happens...

> [... code demonstrating that typeof(b[5])
> CREATES b[5] where, before, no index 5 was IN
> b[]. ...]

I'm not sure that solves the problem.

The OP cites the manual:

>> | https://www.gnu.org/software/gawk/manual/html_node/Type-Functions.html

>> |  Normally, passing a variable that has never been used to a built-in
>> |  function causes it to become a scalar variable (unassigned). However,
>> |  isarray() and typeof() are different; they do not change their arguments
>> |  from untyped to unassigned.

and questions why, if typeof() does "not change [its]
arguments from untyped to unassigned", does a formerly
non-existent array element change from untyped to
unassigned when passed as an argument to typeof()
(paraphrased).

Further, it appears that the OP infers that if typeof()
does not change its arguments from untyped to
unassigned, it also does not CREATE the argument.

So the question is, why is the first ever mention of
b[5] in typeof(b[5]) not treated as "passing a variable
that has never been used" so as to get the benefit of
"typeof() [is] different; [it does] not change [its]
arguments from untyped to unassigned"?

The answer, in practice, is that the unused "variable"
to which that phrase refers is the naked identifier,
not the subscripted element, and once any subscript of
what will become an array is ever created or mentioned
in an array context (even using the subscript existence
test (5 in b) ), the genie is out of the bottle, that
subscripted variable is deemed to have been "used", and
the unsubscripted identifier is forever an array.

But how should he know that from the manual?

Maybe the manual should be clarified a bit:

| Normally, passing a variable that has never been used
| to a built-in function causes it to become a scalar
| variable (unassigned), unless it includes a subscript
| reference, in which case the variable will become an
| array, the element of the array referenced by the
| subscript will be created and that element will have
| the type unassigned.  However, isarray() and typeof()
| are different; they do not change their arguments
| from untyped to unassigned unless the argument
| includes a subscript reference.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]