Skip to Content
Perl To Python Migration
book

Perl To Python Migration

by Martin C. Brown
November 2001
Beginner
320 pages
5h 53m
English
Pearson Business
Content preview from Perl To Python Migration

SORTING SEQUENCES

In Perl we can place the sort function in front of any potential list to sort the results and if we want a sort ordered in the opposite direction then we can use the reverse function. All of the following are common occurrences:

sort @list;
sort keys %hash;
sort grep(/cat/,@names);
reverse sort getlistofcats();

We can of course also qualify the sort further by supplying a block or function to be used when comparing values, with $a and $b providing the two values that need to be compared:

sort { $a <=> $b } @list;
sort currency @money;

The only problem with the Perl solution is that we must make judgements at sort time about whether we are sorting numbers (in which case we use the <=> operator) or strings (when we use cmp ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Perl in a Nutshell

Perl in a Nutshell

Nathan Patwardhan, Ellen Siever, Stephen Spainhour
Perl by Example

Perl by Example

Ellie Quigley
Perl Hacks

Perl Hacks

Chromatic, Damian Conway, Curtis Ovid Poe, Curtis (Ovid) Poe

Publisher Resources

ISBN: 0201734885Purchase book