Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: What is "Schwartzian Transform"

by monkfan (Curate)
on Jul 21, 2005 at 03:56 UTC ( [id://476704]=note: print w/replies, xml ) Need Help??


in reply to What is "Schwarzian Transform" (aka Schwartzian)

I think it is called "Schwartzian Transform". That's why you couldn't find it ;-). Basically it's a sorting technique for arrays of multiple fields. With it you can sort them according to any fields you prefer. Suppose you have this data:
-r--r--r-- 1 yourname 8318 Jan 30 1996 file1.txt -r--r--r-- 1 yourname 11986 Jan 30 1996 file2.txt -r--r--r-- 1 yourname 46852 Feb 27 1996 file3.txt -r--r--r-- 1 yourname 72698 Feb 27 1996 file4.txt
And you want to sort them according to size. You would do it with ST like this:
@sorted_by_size = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [$_, -s] } @files;
So in principle you would do ST in the following step:
1. Map the initial list into a list of ref to lists with the original +and modified values 2. Sort the list of references 3. Map the list of ref back into a plain list with the initial values
Check this out by the very creator himself - Randal Schwartz (merlyn).
Regards,
Edward

Replies are listed 'Best First'.
Re^2: What is "Schwartzian Transform"
by monarch (Priest) on Jul 21, 2005 at 07:51 UTC
    How is the sort named after one of the many perlmonks here any more advantageous than a simple:
    use strict; my @data = ( ['-r--r--r--','1','yourname','8318','2000-01-01','ant.txt'], ['-r--r--r--','1','yourname','11986','1992-12-30','tiger.txt'], ['-r--r--r--','1','yourname','72698','2004-03-03','duck.txt'], ['-r--r--r--','1','yourname','46852','1788-01-26','goose.txt'] ); print( "Which column to sort on?" ); my $column = ( <STDIN> =~ m/^\d+$/ ); exit( 1 ) if ( ! defined($column) ); my @sorted = sort { $a->[$column] cmp $b->[$column] } @data; print( join( "\n", map { join( ",", @{$_} ) } @sorted ) );

    IE, just select the column (or hashref) you want to sort on.

    update: I very much like kelan's answer to this post, it indeed shows one area where this other process comes in useful.

      The usual use is when the sort key must be derived from the data using a process that takes significant time/resources. The idea of the ST is to precalculate all of the sort keys before the sort operation so you only spend the time once:

      @sorted = map { $_->[ 1 ] } sort { $a->[ 0 ] <=> $b->[ 0 ] } map { [ expensivefunc( $_ ), $_ ] } @data;
      Doing a naive sort, you would be calling that expensive function twice for every comparison, which would end up being a lot more than when precalculating:
      @sorted = sort { expensivefunc( $a ) <=> expensivefunc( $b ) } @data;

        In all recent examples, I've moved the "original key" to tuple element 0, so that the first step is consistently  map $_->[0] regardless of how many "expensive" keys are computed.

        Just a minor nit.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

Re^2: What is "Schwartzian Transform"
by GrandFather (Saint) on Jul 21, 2005 at 04:06 UTC

    In that case I won't mention whose node I copied the text from that I pasted into Wikipedia and into the title of my node. :)


    Perl is Huffman encoded by design.

      No reason to perpetuate the error by leaving it uncorrected...

      Update: s/Schwarzian/Schwartzian/

      the lowliest monk

        The good Zaxo has admited to his "finger" problem and corrected the error. See his node below


        Perl is Huffman encoded by design.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://476704]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-20 03:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found