Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWK array initialization

Tags:

arrays

awk

Is it possible to initializate an array in AWK with the common list syntax?

array = [val1, val2, val3]

Or is it obligatory to use the index-value syntax?

array[0] = val1
array[1] = val2
array[2] = val3
like image 465
Hidden Avatar asked Sep 08 '26 13:09

Hidden


1 Answers

No and no. This is how you do it:

$ awk 'BEGIN{split("val1 val2 val3",array); for (i in array) print i, array[i]}'
1 val1
2 val2
3 val3

Read the book Effective Awk Programing, 4th Edition, by Arnold Robbins as if you don't know this then there's a lot of other awk basics you're missing too.

like image 70
Ed Morton Avatar answered Sep 12 '26 22:09

Ed Morton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!