I have a hash that has numeric values. I need to be able to sort out these values and print a header for each NEW value.

my %values = ( "apple" => "1", "pear" => "3", "orange" => "3", "melon" => "2", "grape" => "2");
To yield:
Level 3: Pear, Orange Level 2: Melon, Grape Level 1: apple
Kutsu tried to help with the code on his pad. The idea was to use HOA or AOAs and I haven't any experience with those (or references) so I'm kind of lost. What I have to play with is
my %hash = ( 1 => ["apple"], 2 => ["melon", "grape"], 3 => ["pear", "orange"] ); $hash{1}[1] = "pickle"; print "Level $_\n@{$hash{$_}}\n\n" for reverse sort keys %hash;
Which works. The problem I am having is trying to figure out how this would work with dynamic data. I'll never know what "levels" exist or how many there are, so using indexes probably won't work properly.

In the end, I need to be able to sort and rip parts and pieces of this with ease (like a typical hash). Things to keep in mind are; I need to be able to sort by length of characters in the words, and find specific elements which words start with (or end with) specific characters. Ideally, it'd need to be as maintainable as a hash which I'm sure HOAs and AOAs are capable of.

I'm just new to the multidimensional world and could use some insight.



"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

In reply to Sorting hashes with new value detection by sulfericacid

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.