No such thing as a small change PerlMonks

Perl (specific) Algorithms?

by mr.nick (Chaplain)
 on May 22, 2001 at 04:01 UTC Need Help??

I recently bungled (I think, I won't know for a few days yet) a phone interview because of a combination of nervousness and ignorance. The question was regarding the Schwartzian Transform; which I didn't comprehend the implementation or usefulness of.

So, in the interest of never making the same mistake twice, I seek for some Perl Wisdom.

Are there any other algorithms, implementations or methodologies out there that lend themselves particularly well to Perl? Or perhaps are used commonly in Perl? Or perhas are DESIGNED by someone close to the development of Perl?

I'm also looking for any implementations of standard, well-known algorithms that are particularly clever when done in Perl.

Any pointers or help would be much appreciated. Thank you!

Replies are listed 'Best First'.
Re: Perl (specific) Algorithms?
by japhy (Canon) on May 22, 2001 at 05:49 UTC
Shell programmers are probably the most aware of the concept of a schwartzian transform. They get the results from something like ls -l, sort it somehow, and then use awk to extract a specific field.

Thanks to Perl's built-in hash data type, many idioms (such as intersection, union, and difference computation) are a snap:
```@union = union(\@a, \@b);
@inter = intersection(\@a, \@b, \@c);
@diff  = difference(\@a, \@b);
@in_a  = unique_to(\@a, (\@b, \@c));

sub union {
my %seen;
@seen{@\$_} = () for @_;
return keys %seen;
}

sub intersection {
my %seen;
for (@_) { \$seen{\$_}++ for @\$_ }
return grep \$seen{\$_} == @_, keys %seen;
}

sub difference {
my %seen;
for (@_) { \$seen{\$_}++ for @\$_ }
return grep \$seen{\$_} == 1, keys %seen;
}

sub unique_to {
my %seen;
@seen{@{ shift() }} = ();
delete @seen{@\$_} for @_;
return keys %seen;
}
Finding unique elements in a list is as easy as converting the list to the keys of a hash, and then extracting the keys again.

japhy -- Perl and Regex Hacker
Re: Perl (specific) Algorithms?
by busunsl (Vicar) on May 22, 2001 at 10:48 UTC
There is also the 'Orcish Maneuver', named so because it uses an 'or-cache'. Have a look here or here or here.
Re: Perl (specific) Algorithms?
by VSarkiss (Monsignor) on May 22, 2001 at 19:14 UTC
For a general view of "things done well in Perl", the best book I can recommend is Effective Perl Programming by Joseph Hall and Randal Schwartz (known around the monastery as Saint merlyn).

In particular, the chapter on "Idiomatic Perl" talks about Schwartzian transform, Orcish maneuver, and many other structures that are rendered very elegantly in Perl.

This is a great book. I have a coworker who owns it. He let me borrow it and many things that just weren't 'clicking', just made sense all of a sudden. I am going to buy a copy for myself. I highly recommend this book. It's short, concise, to the point, clear, <insert something else good here>.

Update

Visit Bookpool, you can get this book for \$22.50.

seanbo
Ahh..the odd dog is a strange beast indeed, nobody wants him, but he always seems to be there.
Algorithms in Perl
by John M. Dlugosz (Monsignor) on May 22, 2001 at 18:50 UTC
I really don't know if it's any good—I bought a copy because I think it's a good idea, but have not perused it much. Mastering Algorithms with Perl.

—John

Create A New User
Node Status?
node history
Node Type: perlmeditation [id://82137]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2019-04-19 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I am most likely to install a new module from CPAN if:

Results (106 votes). Check out past polls.

Notices?