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 |