Since you can't know how to sort the totals until they have all been calculated, I think it would be a good idea for you to build an intermediate data structure (an array of hashes would be ideal) containing the name, the two associated values, and the total. Either that, or rearrange your input data into something a little better organized, ie. one set of names that points at both values. Rather than:

%x = ( 'abc' => 2 ) ; %y = ( 'abc' => 3 ) ;

Try something like:

@data = ( { 'name' => 'abc', 'x' => 2, 'y' => 3 }, { 'name' => 'xyz', 'x' => 4, 'y' => 5 } ) ;

I realize this may not be so simple if your data is coming from somewhere else, but keep it in mind nonetheless.

i'm currently running a foreach loop that looks like this:

Er, the one you posted doesn't run. AFAIK, sort keys by %x isn't Perl. Also, your sample data doesn't match your code. I think you'll get better help if you post:


_______________
D a m n D i r t y A p e
Home Node | Email

In reply to Re: help with sort by value by DamnDirtyApe
in thread help with sort by value by rchou2

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.