
mhx
Enthusiast
/ Moderator
Aug 26, 2001, 10:54 AM
Post #4 of 12
(567 views)
|
|
Re: Is this an array of hashes?
[In reply to]
|
Can't Post
|
|
If I do it that way though, $DataStructureRef is a hash reference, not an array reference. Right.
The confusing part for me is the contents of the anonomous array. I know arrays hold strings or references, but what is this trying to hold? In my way of thinking, if it was an array of hash references it would have to be created like this: $DataStructureRef = [ { "field" => "value1"}, {"field" => "value2"}, {"ad" => "nauseum"} ] If you really wanted to build an array of hashes, this would be the right way. Now, arrays always hold so-called SVs, scalar values. A scalar value can be a string, a numeric value, a reference (and if I recall this right from the perlguts manpage even some other things). If the content of an SV is a reference to an array, hash or subroutine, you can use the pointer (->) syntax borrowed from C to actually access the referenced object. Say you have Then $stuff is a SV that holds a reference to an array. You could access the array's elements using:
$$stuff[0] $$stuff[1] $$stuff[2] or, which I find more readable:
$stuff->[0] $stuff->[1] $stuff->[2] Maybe it's because I originate from C programming that I find this more readable, but to me it expresses more explicitly that $stuff is a reference. If you have something more complex, like a reference to an array that holds references to a subroutine, another array and a hash:
my $stuff = [ sub { print 'subroutine' }, [1, 2, 3, 4, 5], {key => 'value'}, ]; Then you can call the subroutine by writing access the array by writing and access the hash table with And, there's also a shortcut so you don't have to type too many ->'s if your structures get too deeply nested:
$stuff->[0](); $stuff->[1][2]; $stuff->[1]{key}; I hope this will make the whole thing a bit clearer. -- Marcus P.S.: Yes, seems there's not so many people around at the moment.
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= ($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"
|