9.1.8 Getting Type Information

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 Strongly Typed Regexp Constants).

"number"

x is a number.

"number|bool"

x is a Boolean typed value (see Boolean Typed Values).

"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 String Type versus Numeric Type.)

"unassigned"

x is a scalar variable that has not been assigned a value yet.

"untyped"

x has not yet been used yet at all; it can become a scalar or an array.

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 Functions), to test if a parameter is an array or not.

NOTE: While you can use isarray() at the global level to test variables, doing so makes no sense. Because you are the one writing the program, you are supposed to know if your variables are arrays or not.

The typeof() function is general; it allows you to determine if a variable or function parameter is a scalar (number, string, or strongly typed regexp) or an array.

We delay further discussion of unassigned vs. untyped to Dynamic Typing In gawk.