Monks, I am still trying to grok array slices, though my understanding is improving thanks to all your help. Now I'm wondering if / how they can /should be used with more complex data structures.
Question 1, can this code be rewritten with an array slice to decrease the repetition in setting my ($color, $fruit)?
Question 2, is this way of dealing with HOA kosher, or am I better off using a HOH here? Would be curious to hear people that love / hate this kind of code, and why.
Thanks for helping me up the learning curve!
use warnings;
use strict;
my ($idx_color, $idx_fruit) = (0,1);
my $hoa = { keyone => ["green","apple"],
keytwo => ["purple","plum"]
};
my ($color, $fruit) = ( $hoa->{keytwo}->[$idx_color], $hoa->{keytwo}->
+[$idx_fruit] );
print "$color $fruit"; # prints "purple plum"
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.