The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
PERL: How do i print an associative matrix? foo.bar Shell Programming and Scripting 2 09-28-2007 10:06 AM
are Associative Arrays possible in UNIX? yongho UNIX for Dummies Questions & Answers 11 07-11-2005 05:58 PM
Perl: Sorting an associative array tine Shell Programming and Scripting 2 10-29-2003 04:11 AM
Associative Array tine Shell Programming and Scripting 4 09-30-2003 10:27 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-05-2006
nervous nervous is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 55
Unable to understand associative nature of awk arrays

About associative nature of awk arrays i'm still confused, not able to understand yet how array element can be accessed based on a string, I got one example at gawk manual to illustrate associative nature of awk arrays, it goes here:
Code
Code:
awk '
# Print list of word frequencies
{
    for (i = 1; i <= NF; i++)
        freq[$i]++
}

END {
    for (word in freq)
        printf "%s\t%d\n", word, freq[word]
}' test.txtIf

this is the input file:
Code:
Kuwait  UAE     Qatar   KSA     Jordan
Lebanon Egypt   Syria   Kuwait  Qatar

Here is the output:
Code:
Syria   1
KSA     1
Lebanon 1
UAE     1
Qatar   2
Egypt   1
Jordan  1
Kuwait  2

Can you pls explain the contents of freq at each iteration of first rule and also what happens at each iteration of END rule? It would be a great favor.

Thanks in advance.

Cheers,
An AWK student
  #2 (permalink)  
Old 10-05-2006
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,557
i want to explain, but my english is not good enough.
maybe this could help.
  #3 (permalink)  
Old 10-05-2006
tayyabq8's Avatar
tayyabq8 tayyabq8 is offline Forum Advisor  
Moderator
  
 

Join Date: Nov 2004
Location: Bahrain
Posts: 579
I have modified your code to illustrate the contents of freq at each iteration:
Code:
$cat freq.awk
#! /bin/ksh
awk '
# Print list of word frequencies
{
    for (i = 1; i <= NF; i++)
{
# Here loop variable i will contain values like 1, 2, 3 until last field of 
# Record which in your case is 5, so $i will behave like $1, $2, $3
# Following condition will lookup in array if key($i ie name of city in your case)
# Already exists in array or not

        if ( $i in freq )     
        print "Key(" $i "): is already there in array and its frequency or value is:  " freq[$i]

# In the 1st iteration of loop, $i($1) is equal to Kuwait, so freq[$i]
# Means freq[Kuwait], because you dont have any value associated to key Kuwait
# Therefor freq[$i] yeilds to zero and ++ operator increases it to 1

        freq[$i]++

# Print the record no., field no. value of key and freq associated to key, at each iteration of loop

        print "Record No." NR "\tField No: " i "\tValue of key: " $i "\tFrequency of word: " freq[$i]
}
print "\n"
}

END {
# Loop thru the whole array(freq) and find the associated value to each key
# Here word denotes key and freq[word] refres value associated to each key

    for (word in freq)
        printf "%s\t%d\n", word, freq[word]
}' abc.txt

Your inputfile:
Code:
$cat abc.txt
Kuwait  UAE     Qatar   KSA     Jordan
Lebanon Egypt   Syria   Kuwait  Qatar

Sample Output:
Code:
$./freq.awk
Record No.1     Field No: 1     Value of key: Kuwait    Frequency of word: 1
Record No.1     Field No: 2     Value of key: UAE       Frequency of word: 1
Record No.1     Field No: 3     Value of key: Qatar     Frequency of word: 1
Record No.1     Field No: 4     Value of key: KSA       Frequency of word: 1
Record No.1     Field No: 5     Value of key: Jordan    Frequency of word: 1


Record No.2     Field No: 1     Value of key: Lebanon   Frequency of word: 1
Record No.2     Field No: 2     Value of key: Egypt     Frequency of word: 1
Record No.2     Field No: 3     Value of key: Syria     Frequency of word: 1
Key(Kuwait): is already there in array and its frequency or value is:  1
Record No.2     Field No: 4     Value of key: Kuwait    Frequency of word: 2
Key(Qatar): is already there in array and its frequency or value is:  1
Record No.2     Field No: 5     Value of key: Qatar     Frequency of word: 2


Syria   1
KSA     1
Lebanon 1
UAE     1
Qatar   2
Egypt   1
Jordan  1
Kuwait  2

Regards,
Tayyab
  #4 (permalink)  
Old 10-07-2006
nervous nervous is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 55
Shereenmotor, Thanks a lot for your detailed reply.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:58 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0