Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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.


Comment on Re: Array / Hash Confusion!
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1069703]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2014-10-31 04:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (214 votes), past polls