I started working up an example solution and quickly realized that I'm unclear on your specification. The question I have is this: Does your hash look like this:

my %hash; @hash{ qw/number score alignment/ } = @cols;

Or does it look like this:

my %hash; @hash{ qw/number score alignment/ } = map{ \$cols[$_] } 0 .. $#cols;

Or does it look like this:

my %hash = ( number => '$cols[0]', scorecons => '$cols[1]' alignment => '$cols[2]' );

In other words, is $cols[0] to be represented in your hash as the value of $cols[0], as a reference to the element (ie, \$cols[0]), or as the literal string '$cols[0]? The last option is unlikely, but the first two are not unreasonable interpretations of your question, and yet they change the look of any example solution.

Please clarify a little for us. Until we have that ironed out we can't really show you how the solution might look.

Also remember that Perl's hashes are unsorted by definition. If you need to present the contents of a hash in some sorted order, you can do that on the fly, but keep in mind that if you refer back to that sorted order frequently you might find it convenient to maintain a separate sorted list; a parallel list, so to speak. In fact, your @cols array could even be an AoA that keeps track of keys and values in a sorted order such as:

@cols = ( [ qw/value1 number / ], [ qw/value2 scorecons/ ], [ qw/value3 alignment/ ] );

You could still maintain the hash too for quick lookups.


Dave


In reply to Re: sorting hashes by davido
in thread sorting hashes by Angharad

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.