Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWK Error: Attempt to use array in a scalar context

Tags:

awk

I am learning AWK. Here is a simple code snippet I tried to split a string into an array and iterate through it.

BEGIN {
  split("a,b,c", a, ",");

  for(i = 1; i <= length(a); i++) {
    print a[i];
  }
}

On running this code, I get the following error:

awk: awk.txt:4: fatal: attempt to use array `a' in a scalar context

However, if I change the for statement to for (i in a) it works just fine. On further trying to understand what this means by Googling it, I see a number of forums (eg: [1]) talking about awk bugs. It will be great if AWK gurus here can help me understand what the error message means.

like image 603
vk239 Avatar asked Sep 10 '12 15:09

vk239


1 Answers

length expects a string argument. You are passing it an array. The error message is telling you that you are using an array where a scalar is expected.

like image 185
William Pursell Avatar answered Nov 15 '22 09:11

William Pursell