I just wanted to follow up and explain _why_ your code doesn't work the way you expect and why it is necessary to use the notation shown by
Tortue,
Ovid, and
lachoy above.
push @$somehash{$foo.$bar}, $x;
# is the same as
push @{$somehash}{$foo.$bar}, $x;
Perl sees
$somehash as a scalar holding a reference to a hash. Paraphrasing from the
Panther, when parsing an expression like this, key lookups are done last. Since the hashref is dereferenced before the key lookups are done, the
@ is applied to return a hash slice.
(aside: With a regular hash
%somehash, a one-element hash slice (which is usually not what you want) would look like
@somehash{$foo}, but with a scalar
$somehash as a hashref, you would use
@$somehash{$foo} or
@{$somehash}{$foo} as above.)
But you didn't want a hash slice-- you wanted to dereference an array reference. In order to do that, you put braces around the scalar holding the array ref. Now we have
@{ $somehash{$foo.$bar} }, which is the notation shown by
the other monks earlier in this thread. The outer braces force the hash lookup to be done before the
@ is applied to dereference the returned value (which, of course, is an array ref). I hope this helps explain what was happening in your code. Also, for a good reference on, well, references, check
tye's excellent
references quick reference (Re: push).
--
sacked
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.