nawk Associative Arrays HELP!

I am trying to create an associative array "table", where the key of an element consists of a line from the "usernames" file, and the value of all elements is 0. I am suppose to use this code snippet but I don't have a clue of how to go about it.
while (getline var<"filename")
tab[var]=0

So far I have created the following awk file asg6.awk which contains the following code:

#!/bin/ksh
BEGIN {
while ( getline var < "$HOME/Assignment6/usernames" )
tab [var] = 0

if($1 in table)
table[$1]++
}
END {
for (i in table)
print table[i],i
}

I enter the following command at the command line but the system just pauses and nothing happens: nawk -f asg6.awk

Can someone please help me out. I am very new to nawk and totally lost.

Thanks.

--------------------------------------------------------

To be more specific on the layout of what my awkfile should contain, here is the format of what my file should look like:
BEGIN {
action_for_set2.a (which is where the while loop w/getline should be inserted)
}

{
action_for_step2.b
}

END {
action_for_step2.c
}
 

Also, do I need to add anything else to my file in order for it to execute properly before the "BEGIN" statement?

Any help would be appreciated.

Thanks.

--------------------------------------------------------

Your obvious effort on this assignment deserves some assistance.
Your script needs to run awk (or nawk).

Within awk, shell variables must be handled differently. ?A $ is used to represent a field (0-199). ?Example, assuming the current line is "the quick brown fox":

print $3 ????# prints brown
fnum=3
print fnum ??# prints 3
print $fnum ?# prints brown

For the last line above, there is a double interpretation: First, fnum is evaluated as 3, then $3 is evaluated as brown.

Shell variables can be passed when awk is invoked with -v option. In the script below, I chose to pull it from the environment with ENVIRON function. ?There are couple other ways also.

Your script has only a BEGIN and an END statement. ?Notice that the layout in your follow-up posting shows:

a BEGIN statement to create the array
a main statement that processes a datafile
an END statement to display summary results

The BEGIN statement creates an array, and the main and END statements need to reference that array by using the same name.

That second (main) statement is the code used to process each line in tina.txt.

The awk command needs to name one or more files, such as:

??awk 'your program' tina.txt

As your specs mentioned, the key of the array is each entire line from usernames (as read into var). ?If any of these lines have more than one field, they can never be matched by field #1 ($1) from tina.txt.

#!/bin/ksh
awk 'BEGIN {
?home=ENVIRON["HOME"]
?userfile=home "/Assignment6/usernames"
?while ( getline var <?userfile )
???table[var] = 0
}

{if ($1 in table)
???table[$1]++
}

END {
?for (i in table)

???print table[i],i
}' tina.txt

--------------------------------------------------------

Thank you so much for responding. I think I've got it now. I've been working on this assignment all weekend and you're input was most helpful. I am moving on to perl now in my assignment (yippee). No, actually, I enjoy programming -- it's just that this class has been jumping around pretty quickly as of late. Anyway, again -- thank you for your help... :)

Have a Unix Problem
Do you have a UNIX Question?

Unix Books :-
UNIX Programming, Certification, System Administration, Performance Tuning Reference Books

Return to : - Unix System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.