Could someone please explain the concept of associative arrays in AWK programming?
Please explain the concept of associative arrays in awk programming with a few examples. I tried to find the good links about the same topic in internet and in our forum, but i did not find any good links to understand/get in-depth knowledge on the same topic. if you know any pointers to the same topic, please do provide.
|
It's really quite simple. Ordinarily, an array has a numeric index:
Code:
fruit[1] = "banana" Code:
colour["banana"] = "yellow" |
And here's the array section of the gawk user's guide.
http://www.gnu.org/software/gawk/man...de/Arrays.html It should be noted that in awk, all arrays are associative. Even numbers like 1,2,3 are stored as simple text strings, not digits. |
Thank you very much for your reply.
Quote:
Could you please explain the behavior of the below awk code.? awk '{ vec[$1]+=1 } END { for (i in vec) {print i vec[i] }' My understanding: The vec[$1] points to the first field in the first line and it stores the same. The vec[$1]+=1 increases the value by 1 that means it proceeds to the next line and captures the first field in the second line. is it correct?. |
I would have to say the easiest process for learning something like this code would be to run it on a small file of known data and see what happens.
|
Just understand that the "$1" will be replaced by the contents of the first field, the result of which will be used as the index string.
So think about it; what happens if the first field on two lines is the same, and what happens if they are different? |
@OP: you may like to know that some langs eg Perl use the term 'hash' (as in hash table, not passwd hashing) for the same technique. :)
|
All times are GMT -5. The time now is 10:38 PM. |