Portable check for array

Gawk has “isarray”:

if (isarray(x))
  print "is array"
  print "is scalar"

However Mawk and “gawk –posix” do not:

fatal: function 'isarray' not defined

This can cause problems:

fatal: attempt to use scalar 'x' as an array


fatal: attempt to use array 'x' in a scalar context

Can Awk detect an array without using the “isarray” function?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

No. If it could then there wouldn’t have been a need for gawk to introduce isarray().

Solution 2

I also don’t think it’s possible.

But I’ll add that with busybox awk, variables can be both arrays and scalar. There it’s OK to do:

a = "foo"; a["foo"] = "bar"

When a variable has been used as an array though, length() returns the number of elements in the array, even if it also has been defined as a scalar (though you can use length(var "") to get the length of the scalar), except when the variable has been passed as an argument to a function and assigned as a scalar there (could be considered as a bug):

$ busybox awk 'BEGIN{a[1] = 1; a = "foo"; print length(a), length(a"")}'
1 3
$ busybox awk 'function f(x) {x = "xxx"; print x[1], length(x)}
               BEGIN{a[1]=1; x = "yyy"; print a[1], length(a); f(a)}'
1 1
1 3

Too bad as otherwise it would have been easy to define a isarray() function there. We can still tell if a variable is an array with at least one element with

function isnonemptyarray(x) {
  return length(x) > 0 && length(x "") == 0

(assuming the variable hasn’t been defined both as an array and scalar)

In anycase, that’s busybox awk specific. length() can’t be used on arrays portably. One can define a portable array_length() function with:

function array_length(a, tmp1, tmp2) {
  tmp1 = 0
  for (tmp2 in a) tmp1++
  return tmp1

But that can’t be used portably on non-array variables.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply