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

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;

Replies are listed 'Best First'.
Re: Re: Re: sort array by value in it
by dragonchild (Archbishop) on May 04, 2004 at 14:59 UTC
    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

        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