LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 05-25-2012, 02:45 PM   #1
AJAY E
LQ Newbie
 
Registered: May 2012
Posts: 2

Rep: Reputation: Disabled
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.

Last edited by AJAY E; 05-25-2012 at 02:46 PM.
 
Old 05-25-2012, 03:19 PM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 1,985

Rep: Reputation: Disabled
It's really quite simple. Ordinarily, an array has a numeric index:
Code:
fruit[1] = "banana"
fruit[2] = "apple"
fruit[3] = "orange"
An associative array can use a string as a sort of index, making the array behave a bit like a key-value store:
Code:
colour["banana"] = "yellow"
colour["apple"] = "green"
colour["orange"] = "orange"
Here's the chapter on arrays from the O'Reilly sed & awk book.
 
1 members found this post helpful.
Old 05-26-2012, 08:30 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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.
 
Old 05-27-2012, 07:55 AM   #4
AJAY E
LQ Newbie
 
Registered: May 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thank you very much for your reply.

Quote:
Originally Posted by Ser Olmy View Post
It's really quite simple. Ordinarily, an array has a numeric index:
Code:
fruit[1] = "banana"
fruit[2] = "apple"
fruit[3] = "orange"
An associative array can use a string as a sort of index, making the array behave a bit like a key-value store:
Code:
colour["banana"] = "yellow"
colour["apple"] = "green"
colour["orange"] = "orange"
Here's the chapter on arrays from the O'Reilly sed & awk book.

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?.
 
Old 05-27-2012, 08:40 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,513

Rep: Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895
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.
 
Old 05-27-2012, 08:56 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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?
 
Old 05-27-2012, 07:01 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
@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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please explain simple linked list concept shariefbe Programming 5 04-23-2011 04:50 PM
LXer: Bash Associative Arrays LXer Syndicated Linux News 0 04-28-2010 10:20 PM
LXer: How To Fake Associative Arrays In Bash LXer Syndicated Linux News 1 06-05-2008 11:01 AM
awk: associative array key names h/w Programming 2 10-17-2007 12:58 AM
using associative arrays corbis_demon Programming 0 01-25-2005 12:47 AM


All times are GMT -5. The time now is 06:54 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration