Hope this helps
Output:
Apples count is: 3
Oranges count is: 3
Pears count is: 3
Src:
#!/usr/bin/perl
use strict;
use warnings;
my $count = 0;
my $key_count = 0;
my %MFN = qw(apples 0 oranges 0 pears 0);
my $MyFruitNames = \%MFN;
INPUTTP: while ( <DATA> ) {
chomp;
last if ( /^\s*_{1,2}\s*end\d{0,}\s*_{1,}/ );
s/^\s+//g;
$_ = lc($_);
$MyFruitNames->{$_}++ if ( defined $MyFruitNames->{$_} );
}
my $MyPrtKey;
foreach $MyPrtKey ( sort keys %{$MyFruitNames} ) {
printf "%s count is: %3d\n",
ucfirst($MyPrtKey),
$MyFruitNames->{$MyPrtKey};
}
__DATA__
Apples
Oranges
Pears
Bananas
Plums
Apples
Oranges
Pears
bananas
kiwi
Apples
Oranges
Pears
bananas
__END0__
From: perl-beginner@yahoogroups.com [mailto:perl-beginner@yahoogroups.com]
On Behalf Of Jeff Soules
Sent: Thursday, June 18, 2009 7:22 AM
To: perl-beginner@yahoogroups.com
Subject: Re: [PBML] Newbie question
In a Unix environment, the easiest way to sort the file would be to
use the sort utility on the file. But you said you're running in
Windows. So, since your code was loading the entire file into an
array (one line per array element) before processing it, you could
then sort that array using Perl's sort() function. This would put the
lines in alphabetical order.
However, per the problem description that you've posted and the
hash-based solution which people have pointed you towards, the line
order in the file shouldn't be a problem. Has your problem situation
changed?
On Wed, Jun 17, 2009 at 6:07 PM, Kevin Patterson<kpatters@...
<mailto:kpatters%40berkeley.edu> > wrote:
>
>
> Ok.
>
> Im stuck again.
>
> I need to sort this input file below before my code..
>
> Cant someone help me with this??
>
>>> #!/usr/bin/perl
>>>
>>> #use warnings;
>>> #use diagnostics;
>>>
>>> my $count = 0;
>>> my $key_count = 0;
>>> $Input_File="crapdata1.txt";
>>> open(INP, $Input_File);
>>> @by_jobname=<INP>;
>>> close(INP);
>>>
>>> foreach $New_Jobname (@by_jobname)
>>> {
>>> $em_job = substr($New_Jobname,11,50);
>>> $em_key = substr($New_Jobname,11,2);
>>>
>>> if ($em_key eq "Apples")
>>> {
>>> $count[1] = $count[1] + 1;
>>> }
>>>
>>> }
>>>
>>> print "$em_key count is: $count[1]\n";
>>>
>>> my $count = 0;
>>> foreach $New_Jobname (@by_jobname)
>>> {
>>> $em_job = substr($New_Jobname,11,50);
>>> $em_key = substr($New_Jobname,11,2);
>>>
>>> if ($em_key eq "Oranges")
>>> {
>>> $count[2] = $count[2] + 1;
>>> }
>>>
>>> }
>>> print "$em_key count is: $count[2]\n";
>>> exit;
>>>
>>> It give me the following results:
>>>
>>> Apple Count is: 2
>>> Oranges Count is: 1
>>>
>>> Here is the data.
>>>
>>> Apples |Sweet
>>> Oranges |Seeds
>>> Apples |Sour
>>>
>>> -----Original Message-----
>>> From: perl-beginner@yahoogroups.com
<mailto:perl-beginner%40yahoogroups.com>
> <mailto:perl-beginner%40yahoogroups.com>
> <mailto:perl-beginner%40yahoogroups.com>
>> <mailto:perl-beginner%40yahoogroups.com>
>> [mailto:perl-beginner@yahoogroups.com
<mailto:perl-beginner%40yahoogroups.com>
> <mailto:perl-beginner%40yahoogroups.com>
> <mailto:perl-beginner%40yahoogroups.com>
>> <mailto:perl-beginner%40yahoogroups.com> ]
>>> On Behalf Of Scot Robnett
>>> Sent: Wednesday, June 17, 2009 12:22 PM
>>> To: perl-beginner@yahoogroups.com
<mailto:perl-beginner%40yahoogroups.com>
> <mailto:perl-beginner%40yahoogroups.com>
> <mailto:perl-beginner%40yahoogroups.com>
> <mailto:perl-beginner%40yahoogroups.com>
>>
>>> Subject: RE: [PBML] Newbie question
>>>
>>> What is the exact format of the file?
>>>
>>> What have you tried so far?
>>>
>>> Nobody is going to write the script for you. The group is here to help
>>> you figure things out when you get stuck or something isn't working, but
>>> you're supposed to try something first.
>>>
>>>
>>>
>>> -----Original Message-----
>>> From: perl-beginner@yahoogroups.com
<mailto:perl-beginner%40yahoogroups.com>
> <mailto:perl-beginner%40yahoogroups.com>
> <mailto:perl-beginner%40yahoogroups.com>
>> <mailto:perl-beginner%40yahoogroups.com>
>>> [mailto:perl-beginner@yahoogroups.com
<mailto:perl-beginner%40yahoogroups.com>
> <mailto:perl-beginner%40yahoogroups.com>
> <mailto:perl-beginner%40yahoogroups.com>
>> <mailto:perl-beginner%40yahoogroups.com> ] On Behalf Of Kevin Patterson
>>> Sent: Wednesday, June 17, 2009 2:11 PM
>>> To: perl-beginner@yahoogroups.com
<mailto:perl-beginner%40yahoogroups.com>
> <mailto:perl-beginner%40yahoogroups.com>
> <mailto:perl-beginner%40yahoogroups.com>
> <mailto:perl-beginner%40yahoogroups.com>
>>
>>> Subject: [PBML] Newbie question
>>>
>>>
>>> Hello..
>>>
>>> I am writing a perl program in windows..
>>>
>>> Here is my problem.
>>>
>>> I have a file containing data such as:
>>>
>>> Apples
>>>
>>> Oranges
>>>
>>> Pears
>>>
>>> Bananas
>>>
>>> Plums
>>>
>>> Apples
>>>
>>> Oranges
>>>
>>> Pears
>>>
>>> bananas
>>>
>>> kiwi
>>>
>>> Apples
>>>
>>> Oranges
>>>
>>> Pears
>>>
>>> bananas
>>>
>>> I write a program to read this file and create a summary report showing
>>>
>>> How many apples:
>>>
>>> How many oranges
>>>
>>> How many Pears
>>>
>>> Can anyone help me??
>>>
>>> thanks
>>>
>>> [Non-text portions of this message have been removed]
>>>
>>> No virus found in this incoming message.
>>> Checked by AVG - www.avg.com
>>> Version: 8.5.374 / Virus Database: 270.12.73/2180 - Release Date:
>>> 06/17/09 05:53:00
>>>
>>> No virus found in this outgoing message.
>>> Checked by AVG - www.avg.com
>>> Version: 8.5.374 / Virus Database: 270.12.73/2180 - Release Date:
>>> 06/17/09 05:53:00
>>>
>>> [Non-text portions of this message have been removed]
>>>
>>> ------------------------------------
>>>
>>> Unsubscribing info is here:
>>> http://help.yahoo.com/help/us/groups/groups-32.htmlYahoo! Groups Links
>>>
>>>
>>
>> [Non-text portions of this message have been removed]
>>
>>
>
> [Non-text portions of this message have been removed]
>
> [Non-text portions of this message have been removed]
>
>
[Non-text portions of this message have been removed]