> I am reading on hashes now and practicing on how to use them. I have not
> taken any perl classes..
>
> I just started reading the books.
If you're learning from books, I cannot recommend _Learning_Perl_
highly enough. It's got a very intelligent presentation order, tons
of useful example exercises, and even an engaging body text.
Good luck.
On Wed, Jun 17, 2009 at 4:50 PM, Kevin Patterson<kpatters@...> wrote:
>
>
> Thanks.
>
> I am reading on hashes now and practicing on how to use them. I have not
> taken any perl classes..
>
> I just started reading the books.
>
> From: perl-beginner@yahoogroups.com [mailto:perl-beginner@yahoogroups.com]
> On Behalf Of Jeff Soules
> Sent: Wednesday, June 17, 2009 1:46 PM
> To: perl-beginner@yahoogroups.com
> Subject: Re: [PBML] Newbie question
>
> You've had prior experience with other programming languages, right?
> Using substr() this way is a very C or Java way of doing things.
>
> Read up on hashes ("associative arrays") and the split() function in Perl.
>
> http://www.tutorialspoint.com/perl/perl_hashes.htm is just one of many
> possible tutorials about hashes.
> http://www.perlmeme.org/howtos/perlfunc/split_function.html seems a
> decent enough split() tutorial.
> I don't claim anything about the quality of these tutorials (I haven't
> actually read them), they just came from the first page of google
> results.
>
> The idea of a hash is that it's like an array, but you can access the
> individual variables by a text-based key, instead of by a numeric
> index. So, instead of having to cycle through your list once for
> apples, once for oranges, once for pears, etc., and then re-write your
> code when your, err, user decides that the program should also work
> for tangerines, instead you can use a hash where the fruit name is the
> key and the count is the value. So you could access your hash as
> $fruitCount{'Apples'} += 1, say. Or even better,
> $fruitCount{$fruitName} += 1;
> Doing the latter, you could build the entire hash table without ever
> needing to know the types of fruit in advance; and you would
> automatically cover every type of fruit mentioned in the input file.
> The only question would be, "hey, how do I get the fruit name?"
> Well...
>
> With the split() function, you can divide your input lines logically,
> instead of by using substr() and hoping that all fruits have the same
> number of characters. split() lets you break a string apart according
> to a delimiter character. In the case of your example here, you have
> the name of a fruit, followed by a pipe |. So that | would be the
> delimiting character; what you really want is the fruit name that
> comes in front of it. If you split each line along the |, then you
> can store the first chunk as the fruit name, and throw away the second
> part (since, at least so far, you don't really care what the rest of
> the line says).
>
> Once you've built up your hash, you'll want to dump its contents. For
> this, you'll need the keys() function, which you'll no doubt find
> plenty of information about online.
>
> One of the Perl mottoes is "There's More Than One Way to Do It"
> (TMTOWDI) and the solution I'm hinting you at here is just one of many
> possible ones (that's what makes Perl fun!) But hopefully this will
> set you on the way to finding one of the many solutions.
>
> Do check back!
>
> On Wed, Jun 17, 2009 at 4:17 PM, Kevin Patterson<kpatters@...
> <mailto:kpatters%40berkeley.edu> > wrote:
>>
>>
>> Sorry...
>>
>> Here is what I wrote:
>>
>> #!/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@yahoogroups.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>
>
>> 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@yahoogroups.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>
>
>> 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]
>
>