in reply to sort array by value in it

Anonymous Monk,
You mean something like this:
#!/usr/bin/perl use strict; use warnings; my @data; while ( <DATA> ) { next if /^host:volume/; chomp; 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"; } __DATA__ host:volume available(MB) %free 10.100.200.1:/dev/hda3 10168 80% 10.100.200.1:/dev/hda2 84 90% 10.100.200.2:/dev/hda3 10168 80% 10.100.200.2:/dev/hda2 84 90%
Cheers - L~R

Replies are listed 'Best First'.
Re: Re: sort array by value in it
by Anonymous Monk on May 04, 2004 at 16:08 UTC
    what i mean is: below is the sorted output
    host:volume available(MB) %free 10.100.200.1:/dev/hda3 10168 80% 10.100.200.2:/dev/hda3 10168 80% 10.100.200.2:/dev/hda2 84 90% 10.100.200.1:/dev/hda2 84 90%

    i have an array containg multiple lines like this one
    10.100.200.1:/dev/hda3  10168 80%
    and i want to sort it on the value of the available field
      Anonymous Monk,
      Did you try the program? All you would have to do is swap $a and $b. I do not remember you saying you wanted it in descending order.

      Cheers - L~R

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