3


I have declared array by splitting string like below.

names = "Dhana,Subha,Siva"
n=split(names,name_arr,",");

I want to print the size of array here without using variable 'n'? is there any way similar to name_arr.length or name_arr.size() ?

Dhanabalan
  • 572
  • 5
  • 19

1 Answers1

4

In my GNU awk system following works, in case as per your question you don't want to use variable to get array's length. We could use length if it is available in your awk.

awk 'BEGIN{names="Dhana,Subha,Siva";split(names, array,",");print length(array)}'

Output will be 3.

Solution 2nd: In case you don't have length option in your awk and you don't want to use num=split(..) option then you have to do it in old way you have to iterate through array and get its count and finally print it then as follows:

awk 'BEGIN{names="Dhana,Subha,Siva";split(names, array,",");for(i in array){val++};print val}'
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93