in reply to Re: Re: Re: sort array by value in it
in thread sort array by value in it

i didnt understand what the
while <DATA>
part was at...
i just didnt understand to put it in the loop where i splitting lines or after it..

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: sort array by value in it
by Limbic~Region (Chancellor) on May 04, 2004 at 16:51 UTC
    Anonymous Monk,
    I do not have the real data that you have, so I improvised using the small snippet you provided. You also have not done an adequete job explaining your problem. I am going to do something Abigail would frown on and guess.
    • You have a list of hosts that you are processing
    • Each host involves a system command to see free disk space
    • The string with imbedded newlines containing the output is stored in a hash where the key is the host
    • When displaying the hosts disk free, you want the string sorted by a substring.
    If that is an accurate guess, you would modify my original code to look something like the following:
    for my $host ( keys %space ) { my @data; for ( split /\n/ , $space{$host} ) { next if /^host:volume/; my @field = $_ =~ /^([^:]+):([^\s]+)\s+(\d+)\s+(\d+)/; push @data, \@field; } for ( sort { $data[$a]->[2] <=> $data[$b]->[2] } 0 .. $#data ) { print join "\t" , @{ $data[$_] }; print "\n"; } }
    Now we are not mind readers and we do not have crystal balls. You are going to need to apply these techniques to your own code and modify accordingly. It would be nearly impossible to give you full blown working code with what little you have provided.

    Cheers - L~R

      Hi L~R. Im just curious about one part of your solution:

      for ( sort { $data[$a]->[2] <=> $data[$b]->[2] } 0 .. $#data ) { print join "\t" , @{ $data[$_] }; print "\n"; }

      Why did you code it this way I wonder? It seems odd that you are actually sorting a list of indexes and not the elements themselves. Theres no speed gain, in fact theres a loss in that each element requires two levels of dereferencing, and you have the added memory hit of constructing a list of indexes. Im really curious if there was any reason why you didnt just say

      print join("\t",@$_),"\n" for sort { $a->[2] <=> $b->[2] } @data;

      or possibly:

      print join "\n",map { join "\t",@$_ } sort { $a->[2] <=>$b->[2] } @dat +a;

      ---
      demerphq

        First they ignore you, then they laugh at you, then they fight you, then you win.
        -- Gandhi


        demerphq,
        Standing by itself, there is no reason. I changed my mind on the data structure I was using several times and what you see there is a hash hastily becoming an array.

        To be quite honest I spent a very long time working on this problem. Not because it was a terribly hard problem to solve but because there was too much room for speculation in the requirements. Finally, I had decided on just presenting a very simplistic example that could be adapted to meet the Anonymous Monk's unspoken requirements. I failed, as you have shown, in reducing it to the basics. This is partly because my mind still hadn't stopped thinking about "what ifs" and also because I am not as great a multi-tasker as I delude myself into believing.

        Cheers - L~R