[H-GEN] perl question about sort()

Jason Parker-Burlingham jasonp at uq.net.au
Fri Feb 28 01:37:07 EST 2003


[ Humbug *General* list - semi-serious discussions about Humbug and     ]
[ Unix-related topics. Posts from non-subscribed addresses will vanish. ]

Bradley Rosser <b_rosser at yahoo.com> writes:

> I think once I tried to do a couple of sorts in a pipeline; something like:
>   @results = sort { ... } map { ... } sort { ... } @data
> and either got a hard error (dumped core or something?) or just bad
> results.

I'm not sure what the point would be but I'll try something just for
the heck of it[1]:

$ perl -w -Mstrict -le'my @data = map { int rand(95)+32 } (1..10);
        my @results = sort { $b cmp $a } map {chr} sort {$a <=> $b} @data;
        $"="\t";
        print "@data";
        print "@results"'
108     92      72      56      41      79      58      113     49      52
q       l       \       O       H       :       8       4       1       )

The point is that the second line of results sorted alphabetically and
is correctly transformed into characters.

My point about this not having much utility is that you've started
with a list, sorted it, changed it, and then sorted it again; the
results of the first sort are lost and have no effect (unless your
first sort routine has some sort of side effect, which would be Bad).

> I know the man page for 'sort' says that sort functions can't be
> recursive, but I don't think I ever found anything saying that you
> couldn't have multiple sorts in serial sequence like this.  Did I miss
> something in the documentation?

Nope.  Not as far as I know.  I've never encountered any syntax errors
or interpreter crashes by pipelining list operators (which is what
you're really doing here).  But that could just be my luck.

> This was with Perl 5 at least 3 years ago; I'm afraid I've forgotten
> the minor version number.

That could have a bearing inasmuch that there may have been Awful Bugs
in the interpreter you were using, but I doubt it.

> Just curious, in case there's a Perl expert who can tell me "yes, it
> was in the documentation", etc.

Well, I'm going to hazard there's no problem with asking the
interpreter to do what you wanted, but it's hard to be sure without
the code you were running or a better idea of the error, and of course
it's a bit much to ask for that now.

My best guess is you're misremembering, or you wrote a sort sub that
had a side effect.

Some of the Perl development lists might be able to give you a better
answer.

jason

[1] : Note that I went against my own advice and did not C<local>ize
      the value of $" (the variable controlling which string is used
      to separate the values of an array inside double quotes).  This
      is okay since I knew nothing else was going to be using it.
-- 
``I may have agreed to something involving a goat.''  -- CJ

--
* This is list (humbug) general handled by majordomo at lists.humbug.org.au .
* Postings to this list are only accepted from subscribed addresses of
* lists 'general' or 'general-post'.  See http://www.humbug.org.au/



More information about the General mailing list