Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Sorting an array of lines

by Quadzilla (Initiate)
on Feb 08, 2002 at 21:28 UTC ( [id://144243]=perlquestion: print w/replies, xml ) Need Help??

Quadzilla has asked for the wisdom of the Perl Monks concerning the following question:

Is there an easy way to sort this example element from my @array on the value called p_inuse?

@array has a bunch of lines that look like this:

10.47.147.0 Free: 63 In-use: 99 P_InUse: 66

I want to sort on the last numeric value (here, the 66) so that I can print the array later in decending order.

Thanks

2002-02-09 Edit by Corion: Removed some CODE tags.

Replies are listed 'Best First'.
Re: Sorting an array of lines
by indapa (Monk) on Feb 08, 2002 at 21:55 UTC
    Take a look at Complex Sorting, it's an excellent explaination of what you need.
    #!/apps/bin/perl -w my (@array, @sorted) =(); while (<>) { chomp; push (@array, $_); } @sorted = map {$_->[0]} sort {$a->[1] <=> $b->[1]} map { my ($sortvalue) = /(\d+)$/; [$_,$sortvalue]; } @array; print "@sorted\n";
      print "@sorted\n";
      Probably not what you want. You chomped the initial list, and $" is usually not set to \n.

      Either don't chomp (don't chomp unless you need the data chomped) and print @sorted; or print "$_\n" for @sorted; or print map "$_\n", @sorted;.

      2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

Re: Sorting an array of lines
by Juerd (Abbot) on Feb 08, 2002 at 21:45 UTC
    Just first-post trolling. In little time, there will be better answers, probably using optimization techniques like the famous Schwartzian Transform.

    print sort { my ($aa) = $a =~ /(\d+)\s*$/; my ($bb) = $b =~ /(\d+)\s*$/; $bb <=> $aa; } <DATA>; __DATA__ 10.47.147.0 Free: 63 In-use: 99 P_InUse: 61 10.47.147.0 Free: 63 In-use: 99 P_InUse: 65 10.47.147.0 Free: 63 In-use: 99 P_InUse: 70 10.47.147.0 Free: 63 In-use: 99 P_InUse: 69 10.47.147.0 Free: 63 In-use: 99 P_InUse: 63 10.47.147.0 Free: 63 In-use: 99 P_InUse: 66

    Have fun

    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

Re: Sorting an array of lines
by CharlesClarkson (Curate) on Feb 12, 2002 at 06:46 UTC

    Since you have some leading spaces in your lines, I used column 8 for the sort instead of column 7. '-8n' means sort descending on column 8 numerically.

    use Sort::Fields; my @sorted = fieldsort ['-8n'], @array;



    HTH,
    Charles K. Clarkson
    Clarkson Energy Homes, Inc.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://144243]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-20 04:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found