[HRPM] Sorting and array of lines returned from 'ps'

Scott Cornette cornette at infi.net
Tue Nov 21 08:30:35 CST 2000


Once you have decided on which field you want sorted, you can try sorting
the array using the perl sort operator with a subroutine passed as an
argument.  This will allow you to do comparisons by choosing whether you
want a numeric or ASCII sort.  For example, to sort a numeric field:

     sub numerically { $a <=> $b; }

     @sorted_array = sort numerically @unsorted_array;

To sort a field of strings (ASCII based):

     sub ascii_string { $a cmp $b; }

     @sorted_array = sort ascii_string @unsorted_array;

There are plenty of examples in the Camel book and Cookbook.

Hope this helps,
Scott Cornette

  -----Original Message-----
  From: owner-norfolk-pm-list at pm.org [mailto:owner-norfolk-pm-list at pm.org]On
Behalf Of Matt Shivers
  Sent: Saturday, November 18, 2000 4:04 PM
  To: norfolk-pm-list at happyfunball.pm.org
  Subject: [HRPM] Sorting and array of lines returned from 'ps'


  Hello all,

  I have an interesting sorting problem. I need to sort an array of lines
returned from the 'ps' command. Each line contains the following
information:

  PID, USER, UID, TIME, %CPU, %MEM, CMD

  I need to sort the array based on the different fields within each line.
For instance, sort based on USER, then sort based on TIME, etc... depending
on how the user wants the information displayed.

  I looked at the built in sorting options available in 'ps', but the 'ps'
sort option doesn't contain all the fields I need to sort on.

  I also looked at the sort function built into Perl, but it looks to be a
simple sort based on the entire element in the array.

  Has anyone sorted in this fashion before?

  Thanks,

  Matt
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/archives/norfolk-pm/attachments/20001121/f8e0258d/attachment.htm


More information about the Norfolk-pm mailing list