Next: I18N Functions, Previous: Bitwise Functions, Up: Built-in [Contents][Index]
gawk
provides two functions that let you distinguish
the type of a variable.
This is necessary for writing code
that traverses every element of an array of arrays
(see Arrays of Arrays), and in other contexts.
isarray(x)
Return a true value if x is an array. Otherwise, return false.
typeof(x)
Return one of the following strings, depending upon the type of x:
"array"
x is an array.
"regexp"
x is a strongly typed regexp (see Strong Regexp Constants).
"number"
x is a number.
"string"
x is a string.
"strnum"
x is a number that started life as user input, such as a field or
the result of calling split()
. (I.e., x has the strnum
attribute; see Variable Typing.)
"unassigned"
x is a scalar variable that has not been assigned a value yet. For example:
BEGIN { # creates a[1] but it has no assigned value a[1] print typeof(a[1]) # unassigned }
"untyped"
x has not yet been used yet at all; it can become a scalar or an array. The typing could even conceivably differ from run to run of the same program! For example:
BEGIN { print "initially, typeof(v) = ", typeof(v) if ("FOO" in ENVIRON) make_scalar(v) else make_array(v) print "typeof(v) =", typeof(v) } function make_scalar(p, l) { l = p } function make_array(p) { p[1] = 1 }
isarray()
is meant for use in two circumstances. The first is when
traversing a multidimensional array: you can test if an element is itself
an array or not. The second is inside the body of a user-defined function
(not discussed yet; see User-defined), to test if a parameter is an
array or not.
NOTE: Using
isarray()
at the global level to test variables makes no sense. Because you are the one writing the program, you are supposed to know if your variables are arrays or not. And in fact, due to the waygawk
works, if you pass the name of a variable that has not been previously used toisarray()
,gawk
ends up turning it into a scalar.
The typeof()
function is general; it allows you to determine
if a variable or function parameter is a scalar, an array, or a strongly
typed regexp.
Next: I18N Functions, Previous: Bitwise Functions, Up: Built-in [Contents][Index]