<snip>
> Conceptually, what you have is a tree. There are
> three
> branches from the root, one for each foul type, and
> each of these is split into a further three
> branches,
> one for each player.
>
Like this:
-->Rodriguez{numFouls}
-->offensive-->Chan{numFouls}
-->Smith{numFouls}
-->Rodriguez{numFouls}
fouls-->personal-->Chan{numFouls}
-->Smith{numFouls}
-->Rodriguez{numFouls}
-->shooting-->Chan{numFouls}
-->Smith{numFouls}
That's what I conceptually see, and what I think it
should be.
> Internally what you have is a hash, %fouls, which
> relates
> foul types (as the hash keys) to hash references (as
> the
> values). Each of these references refer to a hash
> which
> relates player names (as the keys) to foul count (as
> the values).
>
internally, that looks like this, right?
$fouls{offensive}{Chan}
then substitute the foultype and the player name for
the 8 other options.
> It may be helpful to do this
>
> use Data::Dumper;
> print Dumper \%fouls;
>
the call to print is after I've written the hash,
right? and the use Data::Dumper; is just like typing
use strict; right? I don't have to do anything else
with that I suppose?
> which will output Perl code to reconstruct the hash
> contents. It provides a useful visual representation
> of the hash you've built.
>
> > I took a bit of a leap from this earlier example:
> > $fouls{$1}++;
> > Which kept track of the number of fouls for each
> > player, but grouped all fouls together.
> >
> > > which, if $1 eq 'Chan' and $3 eq 'offensive'
> would
> > > increment the element
> > >
> > > $fouls{offensive}{Chan}
> > >
I actually did something like this when I had 1-d
hashes, but six of them, one for every different
foul(for simplicity's sake, I've just written names of
3 diff kinds of fouls), but then I thought I'd have to
write a bunch of lines like you wrote for the 2-d
hash, which I thought would be cumbersome, since there
are plenty of different names.
I had something like:
if ($3 eq 'Offensive')
{
$offensive{$1}++;
}
That's when he suggested that I didn't have to match
$3 or $1 to anything, and so sent me on a chase to
figure out how. That's how I came up with the thought
that I'm not matching the name in the example above
($1) so perhaps I could write a line like so:
$fouls{$3}{$1}++;
and that would do the same thing if wrote:
if ($1 eq 'Chan' and $3 eq 'offensive')
{
$fouls{$3}{$1}++;
}
or something like that.
So this is what I'd like to do, count fouls without
using string comparison operators.
> > > Which is a lot nicer as you don't then have to
> > > declare
> > > a scalar variable for every possible player. Be
> > > careful about upper and lower case characters
> > > though:
> > > as far as Perl's concerned 'Chan' and 'chan' are
> two
> > > different players!
> > >
> >
> > Yup, the nomenclature for the names, fouls and
> > everything with the log is standardized, so I
> won't
> > run into 'chan' and 'Chan.' thanks for the heads
> up
> > though.
> >
> > > > How would I print the %fouls hash out so that
> the
> > > > output would be grouped by type of foul? Here
> is
> > > > an example of the output I'd like to see.
> > > >
> > > > Offensive fouls:
> > > > Chan: 3
> > > > Rodriguez: 1
> > > > Smith: 1
> > > >
> > > > Personal fouls:
> > > > Chan: 1
> > > > Rodriguez: 4
> > > > Smith: 1
> > > >
> > > > Shooting fouls:
> > > > Chan: 1
> > > > Rodriguez: 1
> > > > Smith: 2
> > > >
> > > > would I nest foreach loops? If so, I'm still
> not
> > > > sure how I'd do that.
> > >
> > > You'd have to look at the three subhashes
> > > separately. Try this
> > >
> > > foreach $type ( qw/Offensive Personal
> Shooting/ )
> > > {
> > > printf "%s fouls\n", $type;
> > > my $rank = $fouls{lc $type};
> > > foreach my $player (keys %$rank) {
> > > printf "%s: %d\n", $player,
> $rank->{$player};
> > > }
> > > }
> > >
> >
> > Hmm, it's not.
>
> Not what? :)
>
My mistake, I meant to write that below, it wasn't
clear.
> > I'm getting confused by the variable
> > names and what each represent since the $1 or the
> $3
> > isn't in there.
>
> Scalar $type holds the foul type. The same as $3 on
> input.
> Scalar $player holds the player name. The same as $1
> on input.
>
Yes, but was supposed to assume that further up in the
code I would have assigned $1 to $player, and $3 to
$type? That's where a lot of my confusion came from.
Then, what does $rank refer to? Wait, it looks like
it is assigned to
$fouls{$type}
so then $rank would have a key of foultype, and a
value of player name which is also a key and would
have a value of the number of fouls? Is that it?
and I'm not familiar with this syntax:
%$rank
> > Also, can I do it without the reference?
>
> You can do it without the hash reference but it
> looks more
> confusing that way. You can't get around the fact
> that the
> level 2 hashes are anonymous ones so you need to
> understand
> references.
>
I asked because I'm still moving in baby steps here.
:) References are next.
Before this would work, I'd have to assingn $3 to
$type and $1 to $player, right?
> foreach my $type ( qw/Offensive Personal Shooting/
> ) {
> printf "%s fouls\n", $type;
> foreach my $player ( keys %{$fouls{$type}} ) {
> printf "%s: %d\n", $player,
> $fouls{$type}{$player};
> }
> }
>
Thanks, -stu
> Cheers,
>
> Rob
>
>
>
>
> --
> To unsubscribe, e-mail:
> [EMAIL PROTECTED]
> For additional commands, e-mail:
> [EMAIL PROTECTED]
>
__________________________________
Do you Yahoo!?
Yahoo! Calendar - Free online calendar with sync to Outlook(TM).
http://calendar.yahoo.com
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]