array problem

Dear Experts,

please help me out once again my array concepts is not very clear i have one text file like.
1|usa|hh
2|usa|ll
3|usa|vg
4|uk|nn
5|uk|bb
6|kuwait|mm
6|kuwait|jkj
7|dubai|hh

i want to store the third fied of a text file in he array and after that it should give me some reports like this

third field count
usa 3
uk 2
kuwait 2
dubai 1
it should store the third field in to the array and give me the count how many times any name appeared.the third field can be alphanumeric.
please can any body tell me how to do this using array.

Regards,
Shary

try this:

even I have been looking for a awk script for that its something like
group by columns and then the count.

I have awk to do group by and the sum:

awk -F, '{sum[$1,$2,$3]+=$4} END {for (i in sum) print i, sum[i]}' data.

Thanks
SUmeet

Dear Summit,

thank you so much for replying me i really appreciate you
but please can you expalin me this
{sum[$1,$2,$3]+=$4}
what does this mean in array coz my array concept is weak and the other thing i want to store only 3rd field but in your case it seems to be stored 1,2,3 fileds right .
the data might be 1000 line in my text file so please can you brief it out your command
awk -F, '{sum[$1,$2,$3]+=$4} END {for (i in sum) print i, sum[i]}' data.

Regards,
Shary

A perl solution:

$ cat data
1|usa|hh
2|usa|ll
3|usa|vg
4|uk|nn
5|uk|bb
6|kuwait|mm
6|kuwait|jkj
7|dubai|hh
$
$
$ cat mm.pl
#! /usr/local/bin/perl

open(DATA, "< data") || die "Unable to open file other\n";
while (<DATA>) {
        chomp;
        @fields = split(/\|/);
        $counts{$fields[1]}++;
}
close(DATA);

foreach $word (sort keys %counts) {
        print "value = ", $word, "  count = ",  $counts{$word}, "\n";
}
exit 0
$
$
$
$ ./mm.pl
value = dubai  count = 1
value = kuwait  count = 2
value = uk  count = 2
value = usa  count = 3
$

Dear Experts,

i dont want the script in perl.
so please if its possible try to do other than perl using arrays(not perl).

Regards,

Shary

As per your sample output, you need count of second field not the third one, try this:

awk -F"|" '{ freq[$2]++ } END {
for (word in freq)
printf "%s\t%d\n", word, freq[word]
}' data

dear experts,
thank uou so much it really works.

take care bye
shary