snafu has asked for the wisdom of the Perl Monks concerning the following question:
I know that there are many out there who try and read the documentation provided and find the documentation is written with so much high speak that there are those of us who don't 100% understand what they are reading. I am one of those people right now.
So, with that let me explain what I need to wrap my brain around and I sincerely hope that there is someone here who will be kind enough to explain in lamans terms the explanation. I have been working on a project that uses an HoH. Now, I know that you can reference an HoH the following way:
$hash{key}; # single dimension hash $hash{key1}{key2}; # multiple dimension hash
I understand this full-well. Ok, so what I would like to do is figure out what the heck the arrow operator does as according to the manual using the arrow operator is *not* the same as not using it (but then quickly turns around and seems to contradict itself saying that the arrow operatoris optional to use.
<quote>
Note that $array[$x] is NOT the same thing as $array->[$x ]here:Subroutine calls and lookups of individual array elements arise often +enough that it gets cumbersome to use method 2. As a form of syntactic sugar, the examples + for method 2 may be written: $arrayref->[0] = "January"; # Array element $hashref->{"KEY"} = "VALUE"; # Hash element $coderef->(1,2,3); # Subroutine call The left side of the arrow can be any expression returning a reference, including a previous dereference.
The arrow is optional BETWEEN brackets subscripts, so you can shrink the above down to$array[$x]->{"foo"}->[0] = "January"; This is one of the cases we mentioned earlier in which references coul +d spring into existence when in an lvalue context. Before this statement, $array[$x] +may have been undefined. If so, it's automatically defined with a hash reference so +that we can look up {"foo"}in it. Likewise $array[$x]->{"foo"}will automatically g +et defined with an array reference so that we can look up [0]in it. One more thing here.
</quote>$array[$x]{"foo"}[0] = "January"; Which, in the degenerate case of using only ordinary arrays, gives you + multidimensional arrays just like C's: $score[$x][$y][$z] += 42; Well, okay, not entirely like C's arrays, actually. C doesn't know how to grow its arrays on demand. Perl does.
So what the freak does the arrow operator do!?!?! Because I just get confused from this documentation :). Now, Let me give you folks a little more of a view into the application that I am working on.
I have a reference to an HoH which has the structure:
$$player_dbref{player_name}{ip}{user_id};
Now I find myself in the most awkward position of wanting to add a new dimension to the hash (yes that was sarcasm). Now right now I need to actually reference the hash keys with the actual values IE $$player_dbref{happyplayer}{22.22.22.22} which would yield 265 (the player id), right? But what I decided would be easier is to actually have the hash be unique based off the first key in the hash (as usual) then to be able to reference the rest of the keys below that player key by using a topical key. Ok, so that didn't make sense so let me illustrate:
So usually I'd use $$player_dbref{happyplayer}{22.22.22.22} to get the user id
and to get that I'd have to traverse the hash keys to find the name of a player and then I'd
have to traverse the hash again with the player's name I just got as the key to get the IP
and so on and so forth.
Is there a way to only traverse the hash keys the one time for the player name and then use
the topical references such as:$$player_dbref{player_name}{ip_addr} for the
player's IP and $$player_dbref{player_name}{ip_addr}{id} to get the user id
where you are actually using the words 'ip_addr' and 'id' as topical references of what
you want in the hash keying off the player name? Does this make sense?? :)
Is this what arrow operator is for?!?! If I am right in the way I am doing it and there
is no other way to do it then I will just continue doing what I am doing but if there is
an easier way I'd LOVE to know what it is.
_
_
_
_
_
_
_
_
_
_
- Jim
Insert clever comment here...
update (broquaint): title change (was Lamens terms how-to on HoH refs and the arrow operator)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Lamens terms how-to on HoH refs and the arrow operator
by chromatic (Archbishop) on Dec 27, 2002 at 06:04 UTC | |
|
Re: Lamens terms how-to on HoH refs and the arrow operator
by Paladin (Vicar) on Dec 27, 2002 at 06:10 UTC | |
|
Re: Lamens terms how-to on HoH refs and the arrow operator
by CountZero (Bishop) on Dec 27, 2002 at 07:24 UTC | |
by snafu (Chaplain) on Dec 27, 2002 at 18:27 UTC | |
|
Re: Lamens terms how-to on HoH refs and the arrow operator
by pg (Canon) on Dec 27, 2002 at 06:18 UTC | |
|
Re: Layman's terms how-to on HoH refs and the arrow operator
by roundboy (Sexton) on Dec 27, 2002 at 19:00 UTC |