in reply to Layman's terms how-to on HoH refs and the arrow operator
One thing I find helpful when dealing with complex data structures is to break it down one layer at a time, and get a clear understanding of what each element of each structure actually looks like. You can pretend, for the purposes of this exercise, to be doing OO-Perl, and give a name to the "class" of each element, which can help you draw pictures or write explanations.
So, for your code (assuming I'm understanding it properly):
%$player_dbref is a map from player names to let's
call them Player objects. So
$player = $player_dbref->{$a_name};
To answer your original question, this is the only place where the arrow operator is necessary. But because this is Perl, and TMTOWTDI, you can (and have been) replacing $hr->{k} with $$hr{k}, which is the same thing.
Now from your example, it looks like a Player (a person with a
particular name) can log in from multiple places, each resulting in
in what I'll call a PlayerInstance. Thus:
$player_inst = $player->{$an_ip};
Combining this with the previous item yields
$player_inst = $player_dbref->{$a_name}->{$an_ip}
or
$player_inst = $player_dbref->{$a_name}{$an_ip}
since you can eliminate the arrow after the first dereference.
Finally, a PlayerInstance is what? Well, in your example you don't show anything other than an id, so if you really don't have any other data to store, you don't even need another bunch of references, but have $player_inst above be the (numeric) userid value.
However, I'm going to assume you have other information to store, and then there are two possibilities:
There is only one possible id for each name/ip combination,
in which case a player instance is just a (reference to a )
hash from data keys to values, for example:
$id = $player_inst->{id}; $hitpoints = $player_inst->{hp}; $weapon = $player_inst->{weapon};
Let's call one of these things a PlayerData
The other possibility is that you have multiple possible IDs
per name/ip combination. This seems likely, given your
example. In that case, a player instance is a map from IDs to
PlayerData things. Then:
<it>etc</it>.$playerdata = $player_inst->{$an_id}; $hitpoints = $playerdata->{hp};
Now, you can combine this with the previous items, and
get:
$hitpoints = $player_dbref->{$a_name}{$an_ip}{$an_id}{hp}
But the nice thing is that all those intermediate steps are legal, and there's no reason not to do them if you have a need to. So:
HTH!
--roundboy
|
|---|