I'll try

$count {$word} = $count {$word} + 1;

The $count refers to a hash called %count. As you are working with only one
element of that hash (the key/value pair with the key of $word), you
reference the element as a scalar (hence the $, not %). It's still refering
to the hash, but not the whole hash, just the key/value pair with a key
called $word.

The line is taking the value of the key named $word in the hash called
%count and adding one to that value.

Here is a little walkthrough.

# Create an array with the word list
@words = qw(perl learning perl book car book perl fish);

# iterate through the array, counting the instances of each word
foreach $word(@words) {
        # This is the same line as you are having problems with
        $count{$word} = $count{$word} + 1;
}

On the first iteration, $word = "perl", so a hash called %count is created
(as there isn't one already). The line

        $count{$word} = $count{$word} + 1;

can be rewritten as

        $count{'perl'} = $count{'perl'} + 1;

The value of $count{'perl'} is zero as it has just been defined, 0 + 1 = 1
so a key/value pair is created in the hash which looks like this.

%count = (      'perl' => 1 );

next iteration, $word is learning. %count exists, but the key of 'learning'
doesn't. The has now looks like this

%count = (      'perl' => 1,
                'learning' => 1 );

third iteration, $word = 'perl' again. That key already exists in the hash,
so the line

        $count{$word} = $count{$word} + 1;

could be rewitten as

        $count{'perl'} = $count{'perl'} + 1;

$count{'perl'} = 1, as this was stored in the hash before, so it now looks
like
        
        $count{'perl'} = 1 + 1;

The hash is now updated to look like this

%count = (      'perl' => 2,
                'learning' => 1 );

and so on...

Once you're done counting the words you can print out the values stored in
the hash to get the results.

foreach $key (sort keys %count) {
        print "$key found $count{$key} times\n";
}

This is assigning the name of the keys in the %count hash, sorted in order,
to $key on each iteration. The print line then prints the name of the key,
and the value by returning to the hash to lookup the value it is storing for
the key called $key.

Hope this is clear and of some use. Good luck learning Perl.

John

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
Sent: 20 December 2001 11:53
To: [EMAIL PROTECTED]
Subject: Cant figure out how key's value in a hash is created in program
example.............


Hi there,

Very new to PERL (chapter 5 Learning PERL edition 2!!) but I am determined
to master this!! Or at least be good at it!!

I'm having trouble grasping the code below. I know it's a small snippet but
I think it's important.

Can you please explain the $count variable and how the %count came about?

chomp (@words = <STDIN>); # I understand this line as in you are reading in
all the words

foreach $word (@words){ # for every element of the array @words..........
     $count {$word} = $count {$word} + 1; # or $count {$word}++ # I get a
bit lost here!! Is $count the value of the key $word in a hash?
}

foreach $word (keys %count) {#How did %count come about?
     print "$word was seen $count {$word} times\n";
}

Thanks again
B

****************************************************************************
**********************
The contents of this email and any attachments are confidential.
It is intended for the named recipient(s) only.
If you have received this email in error please notify the system manager or
the 
sender immediately and do not disclose the contents to any one or make
copies.

** eSafe scanned this email for viruses, vandals and malicious content **
****************************************************************************
**********************


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--------------------------Confidentiality--------------------------.
This E-mail is confidential.  It should not be read, copied, disclosed or
used by any person other than the intended recipient.  Unauthorised use,
disclosure or copying by whatever medium is strictly prohibited and may be
unlawful.  If you have received this E-mail in error please contact the
sender immediately and delete the E-mail from your system.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to