Dear Monks, everyday at the Monastery is a new day for me that I enjoy since it accompanies practice for my Perl learning experience. I have learnt a lot ever since I joined and today I am learning complex data structures incited by my reply in this
interaction to this
node. I found myself hitting a roadblock so I wanted to learn Perl advanced data structures in a systematic way to be able to answer such questions myself.
My question here spans Perl references to hashes or arrays stored in an anonymous hash, I solicit an explanation to decloud my confusion of why there is no difference in dereferencing a reference to a hash or array using ${} block in the following example and dereferencing the same with only the infix arrow operator. With "no difference" I mean I don't get an error when strictures are on.
Here is the example:
%hash = (fruit=>'apple', veggie=>'corn');
@array = qw(1 2 3 4 5);
$struct={
hashRef => \%hash,
arrayRef => \@array
};
print ${$struct->{hashRef}}{fruit},"\n"; #using the ${}
print ${$struct->{arrayRef}}[0],"\n";
print $struct->{hashRef}{fruit},"\n"; #can this be better to use?
print $struct->{arrayRef}[0],"\n";
Excellence is an Endeavor of Persistence.
Chance Favors a Prepared Mind.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.