On 2011-07-15 17:58, Matt wrote:
I have a file with lines like so but the number of them is in the
thousands instead of seven lines:
blue
red
red
red
orange
orange
green
I want it to count the occurances of each word it finds in the file.
So output on this small file would be:
blue (1)
red (3
On 15/07/2011 16:58, Matt wrote:
I have a file with lines like so but the number of them is in the
thousands instead of seven lines:
blue
red
red
red
orange
orange
green
I want it to count the occurances of each word it finds in the file.
So output on this small file would be:
blue (1)
red (3)
my %words;
while (<>) {
# read input line by line...
# remove trailing new line
chomp;
my $word = $_;
$words{$word}++;
}
foreach my $word (sort keys %words) {
print "$word ($words{$word})\n";
}
--
you can run the script like this : cat file.txt | perl script.pl
On 11-07-15 11:58 AM, Matt wrote:
The contents of the file are sorted already. Any ideas how to do this? Thanks.
Yes, see attached.
--
Just my 0.0002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communicatio