[comp.lang.perl] multy key sort, black pearl

henkp@ruuinf.cs.ruu.nl (Henk P. Penning) (07/19/90)

Procedure multiSort below does a multikey sort (as far as I can check).
Records in @data consist of fields separated by $;.
Array @sortKey indicates the sorting order.  If @sortKey = (3,0,1)
then field 3 is the primary key, field 0 the secondary, etc.
Special cases like empty data and/or empty sortKey are handled elsewhere.
I think it is a real 'black pearl'.

sub multiSort
  { local(*data,*sortKey) = @_ ;
    local($i,%msort,$_) = 0 ;
    for ( @data )
      { $msort{join($;,(split(/$;/))[@sortKey])} .= $i++ . ',' ; }
    @data = @data[split(/,/,join('',@msort{sort keys %msort}))] ;
  }

If this really is the best way to do multi-key-sort in Perl, or if there
is a better, even more obscure way to do it, it tells me something about Perl.

The example shows that Perl has very strong primitives for arrays and tables.
But the lack of other (user defined) data structures will force me to forever
write one-liners to lump/unlump data in a clever way, or wait, and wait ...
Just like APL, Perl has an inherrent one-line-syndrome.

If you combine this with the pass-by-ref mechanism in which often more is
passed what you want (and sometimes more than you expect), I think
Perl-programmers will run into difficulties in larger (>2000 lines) progs.
In general, a high price in performance and/or programming effort
must be paid for abstraction.

Evidently these remarks are only valid for problems outside the scope
of perl: replace sh, awk, sed, grep, sort. (By the way, where is uniq ?)
But the power of Perl wants me to compare it to C and I see some problems.
How about PerlTalk ? What are the real problems ?

One thing I miss in array operations is the possibility to iterate through
2 or more arrays simulaniously, like @new = zip(EXPR,@one,@two,@three).
It would work wonders together with 'zap' (grep(defined,ARRAYs)).
I feel 'zip' is justified because Larry told us not to iterate through
arrays by a indexing in a (for/while/grep) loop ...

				===  HenkP  ===

-- 
Henk P. Penning, Dept of Computer Science, Utrecht University.
Padualaan 14, P.O. Box 80.089, 3508 TB Utrecht, The Netherlands.
Telephone: +31-30-534106
e-mail   : henkp@cs.ruu.nl (uucp to hp4nl!ruuinf!henkp)