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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With