Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Array / Hash Confusion!

by sundialsvc4 (Abbot)
on Jan 07, 2014 at 21:59 UTC ( #1069703=note: print w/replies, xml ) Need Help??

in reply to Array / Hash Confusion!

Here is, I think, a “big light-bulb realization” about Perl ... at least it was for me.

Variables are most-commonly declared as scalars, which are then used to refer to hashes or arrays.   (Called “hashrefs” and “arrayrefs,” respectively.)   It is not, in my experience, very common to find explicit @array or %hash variables.

Why not?   I think it’s for two combined reasons:   versatility, and consistency.   Especially when more-elaborate data structures are being created ... arrays “of” hashes, hashes “of” arrays “of” hashes, and lots of even-stranger things, all of which are created using references.   (Both an array and a hash can contain “only a single scalar value” in each bucket that they can hold, but since “a reference” is “a single scalar value,” each bucket can “contain” anything-at-all, by reference.   So, you just quickly get into the habit of using references for everything, and Perl’s syntax is designed to make this very easy and expressive.

One thing that can also be done is ... to have more than one hash or array (say ...) that refers to the same piece of data.   For example, you could have an array(ref) which contains references to “something,” and, simultaneously, a hash(ref) which contains references to exactly the same “somethings.”   It lets you create the rough-equivalent of an “index” to that data ... another, faster-for-you way to get to the same piece of data, in two ways.   Perl excels at in-memory data manipulation, and its internal implementations are very fast and robust.

Replies are listed 'Best First'.
Re^2: Array / Hash Confusion!
by chromatic (Archbishop) on Jan 08, 2014 at 18:44 UTC
    It is not, in my experience, very common to find explicit @array or %hash variables.

    Your experience is very different from mine. That'd be like declaring everything as void * in C.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1069703]
[Lady_Aleena]: It feels a bit nice doing something that needed doing afk. Helps get my mind off of on keyboard problems.
[Lady_Aleena]: Now hubby won't stop talking to the new remote control though.

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2017-05-25 20:00 GMT
Find Nodes?
    Voting Booth?