On Monday, June 2, 2003, at 10:06 PM, Stuart White wrote:

This does make it clearer, but not entirely.  Is this
what is happening: the loop starts, and goes
immediately into the if statement.  when the regex
finds a line with "Jump Shot" it stores that in $2,
and the player name in $1.

Yes, this is exactly how the first part works. Of course, we're not using the second capture, so why have it? Also, you don't need the \b since a space follows the word and that is a boundary. So here's a simplified match that does the same thing:


/(\w+) Jump Shot/

The next thing it does,
and I'm not quite sure how, is it populates a hash.
Creating it I understand, populating it not so much.
Maybe if I write it as $linehash{$1} =
$linehash{$1}++; though, that still doesn't clear up
the populating part.
Next, it numbers the line, and somehow figures that
it's not just numbering each line that is within the
hash, but rather each separate line that contains the
set of individual players.  Then it starts all over
again, and overwrites each individual player element
when it needs to be incremented.  This part I
understand.  Can you explain the rest with more
detail?  If not, can you point me in the direction of
a book or resource that talks about this %seek idiom?

All of this is happening at once with $linehash{$1}++, which remember is just a shorthand way to say $linehash{$1} = $linehash{$1} + 1. (Note: There is an equals in there, just like you suspected.) ++ is just short for "add one to it and put it back", with it being the value stored under a player's name.


Let's look at an example. If we just matched 'James' and stuck it in $1:

$linehash{James}++; # which we know means...
$linehash{James} = $linehash{James} + 1; # if this is the first James, that is...
# 1 = 0 (undef, because James didn't have a value) + 1
# next time we match James though, it's...
# 2 = 1 (the value we stored last time) + 1
# and the next time...
# 3 = 2 + 1
# etc.


Kevin probably explained it even better than I did though. Is it starting to make sense yet?

I don't know that I can name an exact resource covering the '%seen idiom', though I can name a lot of examples where it's used. I'm not using any big trick here, this is Basic Perl Hash Operations 101, so anything that covers hashes would probably help clear things up. Have you read Learning Perl? It's the traditional starting point.

Hope that helps.

James


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



Reply via email to