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

Jeff Duffy jeff at alanne.com
Mon Nov 20 22:03:15 CST 2000


On Saturday 18 November 2000 16:04, you wrote:

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

 A qsort won't do what you want for all of the fields, because it's an 
asciibetical sort. That means sorting on USER will work fine, but a 
simple Perl sort on say, PID will return the number 577 before the 
number 6, since it comes first in ascii. 

Try " ps aux | awk '{print $2}' | sort " to see what I mean.

 So now you've established that you need two sorts; a standard 
Perl sort (qsort) and one that returns numbers sorted in numeric order. 
What you need a Schwartzian Transform. I won't go into the details 
here, but you can read about it in recipe 4.15 of the Perl Cookbook, or 
at http://www.5sigma.com/perl/schwtr.html .

 If you're putting each value into an array element from the output of 
ps, you can build an array of array refs, and sort the dereferenced 
array field that corresponds to the field you wish to sort on to 
produce your output table. That calls for some fairly tricky bits of 
reference manipulation, but it works nicely for building tables on 
the fly.
 
 Chris probably has an easier answer for you.

Jeff

-- 
For most people, the perceived usefulness of a computer language is
inversely proportional to the number of theoretical axes that the 
language attempts to grind.
				--Larry Wall



More information about the Norfolk-pm mailing list