R
Rodrick Brown
Hello,
Just learning Perl so bare with me.
I have the following output file:
pear
apple
apple
orange
mango
mango
pear
cherry
apple
ill would like the count the ammount of occurances for each fruit.
I spent a few hours trying to do this and just gave up if someone can help
me out with an example or a better way to do this than the method i'm trying
to use
This is as far as I got
#!/usr/bin/perl -w
use strict;
my @keys;
my @fruits;
my %cnt;
my $types;
my $f = 1;
my $m;
open(LOG,"/tmp/fruits.txt") || die("Can't open file: $!\n");
while(<LOG>)
{
next if(/^\s+/);
push(@fruits,$_);
}
# Give all fruits a default value of 1
foreach my $types (@fruits)
{
$cnt{$types} = $f;
}
foreach $types (@fruits)
{
@keys = keys %cnt;
while(@keys)
{
my $fruitnames = pop(@keys);
if($types =~ m/$fruitnames/)
{
$cnt{$types}++;
print "$cnt{$types} $fruitnames";
}
}
}
The code doesnt work and i'm a bit fustrated that I couldnt get it working,
many times I thought I had it but I never did get the results I expected.
Just learning Perl so bare with me.
I have the following output file:
pear
apple
apple
orange
mango
mango
pear
cherry
apple
ill would like the count the ammount of occurances for each fruit.
I spent a few hours trying to do this and just gave up if someone can help
me out with an example or a better way to do this than the method i'm trying
to use
This is as far as I got
#!/usr/bin/perl -w
use strict;
my @keys;
my @fruits;
my %cnt;
my $types;
my $f = 1;
my $m;
open(LOG,"/tmp/fruits.txt") || die("Can't open file: $!\n");
while(<LOG>)
{
next if(/^\s+/);
push(@fruits,$_);
}
# Give all fruits a default value of 1
foreach my $types (@fruits)
{
$cnt{$types} = $f;
}
foreach $types (@fruits)
{
@keys = keys %cnt;
while(@keys)
{
my $fruitnames = pop(@keys);
if($types =~ m/$fruitnames/)
{
$cnt{$types}++;
print "$cnt{$types} $fruitnames";
}
}
}
The code doesnt work and i'm a bit fustrated that I couldnt get it working,
many times I thought I had it but I never did get the results I expected.