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

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

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: sort array by value in it
by dragonchild (Archbishop) on May 04, 2004 at 15:54 UTC
    First, get those IP addresses off a public site. Think before you post!

    Second, you didn't understand the code I provided.

    1. Why are you setting $_ in the first line of the loop?
    2. Check your intendation in the map. It looks like you think the regex and hashref are part of the foreach-loop, not the map.
    3. This will provide a line in @out for each line in @lines. Make sure that's what you want.
    4. This will not skip lines as you were doing with the if-statement. Personally, I would do that kind of parsing in the do_it() function. But, you can do it in the map.
    @lines = map { /(\/\w+\/\w+)\s+\w+\s+\d+\s+\d+\s+(\d+)\s+(\d+)%/; if ($1) { { volume => $1, available => $2, free => 100 - $3, } } else { () } } split(/\n/, $space{$host});

    I hope you're testing this in some test environment ...

    ------
    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