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):

  1. %$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.

  2. 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.

  3. 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:

    1. 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

    2. 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:

      $playerdata = $player_inst->{$an_id}; $hitpoints = $playerdata->{hp};
      <it>etc</it>.

    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:

To find all the IPs for a particular player
keys %$player, or keys %{$player_dbref->{$a_name}}
To find all the IDs for a player at a particular IP (assuming there's more than one)
keys %$player_inst, or
keys %{ $player->{$an_ip} }, or
keys %{ $player_dbref->{$a_name}{$an_ip} }

HTH!

--roundboy


In reply to Re: Layman's terms how-to on HoH refs and the arrow operator by roundboy
in thread Layman's terms how-to on HoH refs and the arrow operator by snafu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.