[thelist] Evolt Web Design Directory

dwayne dwayne at mentia.com
Tue May 15 13:48:35 CDT 2001


On Tuesday 15 May 2001 18:04, Simon Coggins wrote:
> Currently anyone can submit links and categories - just click on "suggest
> a link" or "suggest a category" at the bottom of the relevant
> category. 

oops. too fast on the send button...sorry, didn't mean to spam thelist with 
links. i'll add those, of course.

payment (been saving this one for just such an occasion)...

<tip type="perl">
map is cool. it lets you take a whole list of values and pass them through a 
function or a block of code. while it's handy for obfuscated messages:

map( {print chr($_)} qw/97 108 108 32 121 111 117 114 32 98 97 115 101 32 97 
114 101 32 98 101 108 111 110 103 32 116 111 32 117 115/);

it can also improve the efficiency of your sorts:

sort ( {lc $a eq lc $b} @list )

is all well and good, but the lc operation has to be done each time two items 
are compared (14 000 in 1 000 element list). use map to reduce the 
bottleneck:

#! /usr/bin/perl -w
use strict;

my @list = qw/One fish Two Fish red FISH blue fish/;

# first, create a temporary array of anonymous arrays with
# the computed value and the original value
my @temp = map( { [lc $_, $_] } @list );

# sort the temporary array
@temp = sort({$a->[0] cmp $b->[0]} @temp);

# and map the results to a sorted array...
my @sorted = map( { $_->[1] } @temp);

my $i = 0;
for (@sorted) {
    print "$i = $_\n";
    $i++;
}

(with many thanks to the Perl Cookbook)

</tip>




More information about the thelist mailing list