The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




Thread: array problem
View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #4 (permalink)  
Old 02-09-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,127
A perl solution:

Code:
$ 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
$