in reply to help with sort by value

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

Replies are listed 'Best First'.
Re: Re: help with sort by value
by rchou2 (Novice) on Jul 19, 2002 at 14:00 UTC
    That won't work because I need to do the computation within the foreach loop based on previous code.....Is there anyway I can use sort() with the print() statement?

      How can you sort something you haven't had the opportunity to calculate yet? And, why is this important? Like I said, please post some code.


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