Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2018-03-20 15:43 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (254 votes). Check out past polls.