When you want an association between a key and a clump of data, a hash (Perl for Java maps) is just the thing. You can assign the keys of a hash a value consisting of any scalar or reference. If you only want to map a single value to each key (e.g. just the max) then a scalar will do. If you want to map multiple values to a key (e.g. both a min and a max) you will need to assign each hash key a reference to an array or hash. Which you choose is largely a matter of style, though hashes of hashes tend to be more maintainable if the kinds of data associated with a key are likely to expand or change over time.
# hash of hashes
$hRecords{$key}{min} = $minSoFar;
$hRecords{$key}{max} = $maxSoFar;
# hash of arrays
$hRecords{$key}[0] = $minSoFar;
$hRecords{$key}[1] = $maxSoFar;
# or better (self documenting; less error prone)
my $MIN_IDX=0;
my $MAX_IDX=1;
$hRecords{$key}[$MIN_IDX] = $minSoFar;
$hRecords{$key}[$MAX_IDX] = $maxSoFar;
For specifics about constructing and retrieving data from hashes of arrays and hashes of hashes, see perldsc
Best, beth
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.