Theiling Online    Sitemap    Conlang Mailing List HQ   

Re: lHow to do a reverse word list in Perl

From:Mark Reed <markjreed@...>
Date:Friday, October 27, 2006, 12:59
I saw the subject and thought you meant a translating list in the
opposite direction.  Why would you want to sort a word list in
alphabetical order according to the backward spelling of the word?  I
feel I'm missing an application here.

Btw, this is a classic case where the Schwartzian Transform will speed
things up if you have a long list.  The sort you give reverses each
word every time it's compared to any other word.  So if you have 50
words, each one gets reversed 49 times, for a total of 2450 calls to
reverse(), which aint all that cheap an operation.

Computers and Perl are fast enough these days that it'll probably
still seem instantaneous, in which case, great.  Leave it alone -
better to be able to understand the code readily.  But if you have a
lot of words and it slows down noticeably, try this:

map { $_->[1] }
sort { $a->[0] cmp $b->[0] }
map { [ reverse($_), $_ ] } <>

In place of the

sort { reverse($a) cmp reverse($b) } <STDIN>

(<> Is more flexible than <STDIN>.  It works the same if you pass no
arguments to your script, but you can also pass a filename, in which
case the wordlist is read from that file...)

On 10/27/06, Benct Philip Jonsson <bpjonsson@...> wrote:
> #!/usr/bin/perl -w > > foreach $wort (sort {reverse($a) cmp reverse($b)} <STDIN>) { > write; > } > > format = > @>>>>>>>>>>>>>>>>>>>>>>>>>>>>> > $wort > . > __END__ > > Found it at > <http://userpage.fu-berlin.de/~corff/perl/Beispiele/maketongoruu.pl> > and just found it beautiful! :-) > -- > > > /BP 8^)> > -- > Benct Philip Jonsson -- melroch at melroch dot se > > a shprakh iz a dialekt mit an armey un flot > > (Max Weinreich) >
-- Mark J. Reed <markjreed@...>

Replies

Dirk Elzinga <dirk.elzinga@...>
Henrik Theiling <theiling@...>How to do a reverse word list in Perl