Re: sort a table

2003-01-30 Thread Carl Jolley
On Wed, 29 Jan 2003, Andrew Mansfield wrote:

> Thanks for the tips about command line arguments, working great..
>
> however I'm faced with another problem in which walking thru my O'Reilly
> books & perldocs isn't made clear enough for me..
>
> I have a list of data read in from a text file in which each line has the
> following format:
>
> time status type first_name last_name machine_name
>
> where each value is separated by a space. I want to do a multi-sort by the
> various field types
>
> for example: last_name then first_name then type then status then time.
>
> Do I need to split each line into an array first and do something or can I
> sort by setting the $a and $b to match each of the fields in succession..
>
> Here's what I tried out as an experiment to sort by machine_name, trying to
> use \w$ to match the last word without splitting the line:
>
> my @userdata=sort {$a =~ /\w$/ cmp $b =~ /\w$/} @userlist; # where userlist is the 
>unsorted list.
>
> It doesn't work: the list comes out as if it was sorted on the time field.
>
> Is there a better way to do this?
>

I suggest that you take the time to learn the differce between
comparison operators and regex match operators including the appropriate
syntax in whith each is used.

 [EMAIL PROTECTED] 
 All opinions are my own and not necessarily those of my employer 

___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs



Re: sort a table

2003-01-29 Thread Randy W. Sims
On 1/29/2003 1:14 PM, Joseph P. Discenza wrote:

Andrew Mansfield wrote, on Wednesday, January 29, 2003 13:00
: I have a list of data read in from a text file in which each line has the
: following format:
: 
: time status type first_name last_name machine_name
: 
: where each value is separated by a space. I want to do a multi-sort by the
: various field types
: 
: for example: last_name then first_name then type then status then time.

You want to do a split, then sort on the pieces of the split. I suppose you
want to write back out the whole line?

Use the Schwartzian Transform (I got this from _Effective_Perl_Programming_,
Hall & Schwartz), which uses map to turn each data line into an anonymous
array, then sorts using the bits of the array, and finally map again to get
the original back:

There was a reference in the December issue of SysAdmin Magazine 
 to a very informative paper on sorting 
 that you might find 
interesting.



___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: sort a table

2003-01-29 Thread Joseph P. Discenza
Andrew Mansfield wrote, on Wednesday, January 29, 2003 13:00
: I have a list of data read in from a text file in which each line has the
: following format:
: 
: time status type first_name last_name machine_name
: 
: where each value is separated by a space. I want to do a multi-sort by the
: various field types
: 
: for example: last_name then first_name then type then status then time.

You want to do a split, then sort on the pieces of the split. I suppose you
want to write back out the whole line?

Use the Schwartzian Transform (I got this from _Effective_Perl_Programming_,
Hall & Schwartz), which uses map to turn each data line into an anonymous
array, then sorts using the bits of the array, and finally map again to get
the original back:

@sorted = map { $_->[0] }
  sort { $a->[5] cmp $b->[5] ||
 $a->[4] cmp $b->[4] ||
 $a->[3] cmp $b->[3] ||
 $a->[2] cmp $b->[2] ||
 $a->[1] cmp $b->[1] }
  map { [$_, split] }
  ;

The inner (bottom) map puts the original into the 0 position, and splits the
data (read from filehandle IN) into the remaining positions. Then you dereference
the anonymous arrays $a and $b, pulling out the comparators. The "$a cmp $b || ..."
notation is standard for multi-criteria sorting.

I've assumed that your time stamp is a consistent format and can be sorted by cmp
(for example, 2003:01:29:08:14:37.256 or something similar), and that type and
status are alpha, not numbers (if they're numbers, replace "cmp" with "<=>").

Good luck,

Joe

==
  Joseph P. Discenza, Sr. Programmer/Analyst
   mailto:[EMAIL PROTECTED]
 
  Carleton Inc.   http://www.carletoninc.com
  574.243.6040 ext. 300fax: 574.243.6060
 
Providing Financial Solutions and Compliance for over 30 Years
* Please note that our Area Code has changed to 574! * 

___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs