in reply to sort array by value in it

I'd change your code slightly to make your array into an array of hashes.
@lines = map { /(\/\w+\/\w+)\s+\w+\s+\d+\s+\d+\s+(\d+)\s+(\d+)%/; { volume => $1, available => $2, free => 100 - $3, } } split /\n/, $space{ $host }; # This is the sorting line! @lines = sort { $a->{available} <=> $b->{available} } @lines; push @out, "$host:$_->{volume} $_->{available} $_->{free}%" for +@lines;

------
We are the carpenters and bricklayers of the Information Age.

Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Replies are listed 'Best First'.
Re: Re: sort array by value in it
by Anonymous Monk on May 04, 2004 at 14:51 UTC
    now i getting this funny looking warnings
    host:volume available(MB) %free Use of uninitialized value in subtraction (-) at freespace line 43. Use of uninitialized value in subtraction (-) at freespace line 43. Use of uninitialized value in numeric comparison (<=>) at freespace li +ne 50. Use of uninitialized value in numeric comparison (<=>) at freespace li +ne 50. Use of uninitialized value in numeric comparison (<=>) at freespace li +ne 50. Use of uninitialized value in numeric comparison (<=>) at freespace li +ne 50. Use of uninitialized value in concatenation (.) or string at freespace + line 52. Use of uninitialized value in concatenation (.) or string at freespace + line 52. Use of uninitialized value in concatenation (.) or string at freespace + line 52. Use of uninitialized value in concatenation (.) or string at freespace + line 52. 149.153.130.11:/dev/hda3 10168 80% 149.153.130.11:/dev/hda2 84 90% 1 149.153.130.11: 100% 2 149.153.130.11: 100% Use of uninitialized value in subtraction (-) at freespace line 43. Use of uninitialized value in subtraction (-) at freespace line 43. Use of uninitialized value in numeric comparison (<=>) at freespace li +ne 50. Use of uninitialized value in numeric comparison (<=>) at freespace li +ne 50. Use of uninitialized value in numeric comparison (<=>) at freespace li +ne 50. Use of uninitialized value in numeric comparison (<=>) at freespace li +ne 50. Use of uninitialized value in concatenation (.) or string at freespace + line 52. Use of uninitialized value in concatenation (.) or string at freespace + line 52. Use of uninitialized value in concatenation (.) or string at freespace + line 52. Use of uninitialized value in concatenation (.) or string at freespace + line 52. 149.153.130.23:/dev/hda3 10168 80% 149.153.130.23:/dev/hda2 84 90% 3 149.153.130.23: 100% 4 149.153.130.23: 100%
    also the lines numbered 1 2 3 4 above shouldnt be appearing
    where lines 43 50 and 52 are
    @lines = map { /(\/\w+\/\w+)\s+\w+\s+\d+\s+\d+\s+(\d+)\s+(\d+)%/; { 43 volume => $1, available => $2, free => 100 - $3, } } split /\n/, $space{ $host }; #This is the sorting line! 50 @lines = sort { $b->{available} <=> $a->{available} } @lines; 52 push @out, "$host:$_->{volume} $_->{available} $_->{free +}%" for @lines;
      Doesn't surprise me. Code posted on Perlmonks, unless otherwise stated, is completely untested. Remember - you didn't give us the whole script, so I have no idea what you plugged my snippet into.

      Additionally, did you even try to understand the snippet before plugging it into your code?

      ------
      We are the carpenters and bricklayers of the Information Age.

      Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

      I shouldn't have to say this, but any code, unless otherwise stated, is untested

        yea tried to understand the code before i used it think i understand it.
        here is what i plugged it into..
        it just a simpole script that uses mobile agents to relocate to another machine then run the code in do_it on that machine and returns a scalar containg the contents of df -Tm
        these are then stored in a hash this is done for a couple of machines then the contents of the hash must be sorted to display a ranked listing of the machines with the most space available
        #! /usr/bin/perl -w # # drivespace # use strict; use Mobile::Executive; my %space; ###################################################################### +###################################################### sub do_it { return scalar `df -Tm`; } ###################################################################### +####################################################### relocate( 'xxx.xxx.xxx.xxx', 2001 ); $space{ 'xxx.xxx.xxx.xxx' } = do_it; relocate( 'xxx.xxx.xxx.xxx', 2001 ); $space{ 'xxx.xxx.xxx.xxx'} = do_it; #relocate( 'xxx.xxx.xxx.xxx', 2001 ); #$space { 'xxx.xxx.xxx.xxx'}= do_it; #relocate( 'xxx.xxx.xxx.xxx', 2001 ); #$space = { 'xxx.xxx.xxx.xxx'}do_it; #relocate( 'xxx.xxx.xxx.xxx', 2001 ); #$space = { 'xxx.xxx.xxx.xxx'}do_it; relocate( 'xxx.xxx.xxx.xxx', 2001 ); my $counter = 1; my @out; my @lines; my $disk_used = 0; my $free = 0; print "host:volume available(MB) %free\n"; foreach my $host (keys %space) { $_ = $space{$host}; @lines = map { /(\/\w+\/\w+)\s+\w+\s+\d+\s+\d+\s+(\d+)\s+(\d+)%/; { volume => $1, available => $2, free => 100 - $3, } }split /\n/,$space{$host}; #This is the sorting line! @lines = sort { $b->{available} <=> $a->{available} } @lines; push @out, "$host:$_->{volume} $_->{available} $_->{free +}%" for @lines; }

        Edited by Chady -- removed real IPs