Allowing hashes with duplicate keys would be a great way to improve Perl.

Sorry Anonymonk, but this doesn't make much sense (to me)?

If the hash you show at the top of your post retained duplicate keys, and I printed print $mapdup{1}; what would be printed?

What you are showing is not a hash with duplicate keys, it's a hash with multiple values for a given key.

The answer to the question above is either you want all the values for the specified key to be printed, in which case, use a HoA as you show, or even just concatenating the values into a single string if you always wanted to use them as a single entity.

However, given the values and structure you show in your %mapdup hypothetical duplicate-keys array, what you actually want is a 2d matrix of data, with the individual values selectable by two keys.

1) the numeric value. 2) the language.

You show several ways that this could be done, though your "HASH OF LISTS" and "HASH OF ARRAYS" are--barring a little unnecessary punctuation on the former--identical.

However, if the need is to map all numeric values to their textual equivalents in each of the languages, which isn't what you have shown, but it would seem a strange optimisation to omit some numbers--as you may need them later--and an even stranger thing to store different subsets of numbers in each of the languages.

You did miss what would seem to me the most obvious solution given that one key is numerical and the other textual a HoA's where the numeric value is a direct index to the required value.

my @map = { English=>[ qw[one two three four five] ], French=>[ qw[ une deux troix quatre cinq] ], ... }; print $map{French}[21]; # Gives deux

Which seems to encapsulate the requirements about as concisely as it possible to do. If you really are concerned about space and will never use 'three' or 'four', you could always set these values to 'undef' which would reduce the storage requirements.

I wouldn't consider this "working around a limitation", just using the tools provided in the right way.

If you have a better example of where "duplicate keys" would be useful, I'd like to see it. Off the top of my head I find it difficult to envisage an application for them or even how they might work.


Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke.

In reply to Re: Re: 2 dimensional array sorting... by BrowserUk
in thread 2 dimensional array sorting... by Soko

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.