List Info

Thread: Counting words




Counting words
user name
2006-04-26 15:27:59
Sharp, Craig wrote:

> I have a file that contains many li˜es.  Here is an
example:
> 
> evaluate         eaprod              7844       2    
15688         0
>         2
> evaluate         agncyis             7347       2    
14694         0
>         1
> agncyis          compstr             1117       2      
  0         0
>         0
...
> opncon           auditrec            8842       1     
8842         0
>         1
> 
> I need to loop through and find the first word in each
line and count
> the number of lines that contain this word in the first
position.
>  Example using the above list:
> 
> Evaluate 17
> Agncyis 1
> Opncon 4
> 
> I need to do this for each unique word in the first
positon.

That's like a couple lines of code - untested example :

my %hash;
open IN, ...
while (<IN>) {
	my f = split ' ', $_;
	$hash{lc $f[0]}++;	# I forced lowercase
}
close IN;

print "$_ $hash{$_}\n" foreach keys %hash;
_______________________________________________
Perl-Unix-Users mailing list
Perl-Unix-Userslistserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )