Forum archive 2000-2006

Michael Gage - PGsort

Michael Gage - PGsort

by Arnold Pizer -
Number of replies: 0
inactiveTopicPGsort topic started 11/16/2002; 7:30:55 AM
last post 11/16/2002; 7:30:55 AM
userMichael Gage - PGsort  blueArrow
11/16/2002; 7:30:55 AM (reads: 1652, responses: 0)

Description

General sorting macro

Syntax

PGsort( &sort_subroutine, @list);

Params

&sort_subroutine is a subroutine of two variables which defines order. It's output must be -1, 0, 1 depending on whether the first variable is less than, equal to or greater than the second.
@list is the list to be sorted.

Returns

Returns list of elements in @list, ignoring duplicates. The order is unspecified.

Examples

join(" ",PGsort( sub {$_[0] <=> $_[1]} , (23,2,10,11,11,31); ); returns "2 10 11 11 23 31"

Notes

Using the perl sort directly in problems is not allowed for two reasons. The first is that the hardwired $a and $b variables used by sort can interfere with other values assigned to $a and $b. The second is that it is pretty easy to create closed loops with sort that hang the program. Currently nothing is done to prevent this, but later we may wish to modify num_sort and lex_sort to catch this problem.

See Also

lex_sort
num_sort
uniq

Macro Definition File

PGbasicmacros.pl

 

<| Post or View Comments |>