[NBLUG/talk] I hate perl. :-)
Chris Palmer
chris at eff.org
Tue May 30 10:38:23 PDT 2006
William Tracy writes:
> Nice. :-) I'm not familiar with the curly brace syntax you used, which
> probably means I need to read some more.
Which curly brace syntax? There is the map expression:
print map { $_ . "\n" } sort keys %ngrms;
map's first argument can be a subroutine reference ("coderef" in Perl
terms), and literal subs can be used where references are called for.
The sub literal
{ $_ . "\n" }
simply concatenates its first argument with "\n"; and in Perl land, the
value of a subroutine call is the value of the last expression evaluated
in the sub, unless there is an explicit return statement.
Unfortunately, { ... } can also denote a literal hashref. :\ From the
perldoc for map:
> "{" starts both hash references and blocks, so "map { ..." could be
> either the start of map BLOCK LIST or map EXPR, LIST. Because perl
> doesn't look ahead for the closing "}" it has to take a guess at which
> its dealing with based what it finds just after the "{". Usually it
> gets it right, but if it doesn't it won't realize something is wrong
> until it gets to the "}" and encounters the missing (or unexpected)
> comma. The syntax error will be reported close to the "}" but you'll
> need to change something near the "{" such as using a unary "+" to
> give perl some help:
Oh, the pain.
> >The obvious performance improvement is to turn the recursion into
> >iteration. This version is very slow.
>
> I thought about that a little. The performance I got was almost as
> fast as the console can print I/O, but that doesn't mean there's no
> room for improvement. :-)
I see you only tried it with arguments of, say, 5 letters or less. Try
getting the anagrams of the word "insiginificant". ;)
--
https://www.eff.org/about/staff/#chris_palmer
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 186 bytes
Desc: not available
Url : http://nblug.org/pipermail/talk/attachments/20060530/7c5dd3ec/attachment.pgp
More information about the talk
mailing list