7 comments

[ 0.27 ms ] story [ 31.4 ms ] thread
I've found $seen{$_}++ and similar constructs made out of hashes incredibly useful, but having built-in hashes is itself incredibly useful.

For example, when I first heard about them, I didn't understand Markov chains at all. Then I saw some example code that was only a few lines long and which used hashes to implement them. In about an hour, I had my own nonsense text generator up and running. It's not very different, conceptually, from using a hash to remember what you've "seen"--the only difference is that the hash key is a chunk we've seen, and the hash value contains everything that could possibly come next (and multiple copies of it, so that common text is more likely).

And then there's how sorting things can simplify them. Want to find anagrams? Inexperienced folks might try to program something that generates every permutation and tests for equality. That's slow and horrible. Just sort the letters first and compare. If they sort to the same string, they're anagrams. But you don't actually have to sort, you can just turn them into a vector (an array where the first element is the number of As, the second the number of Bs, etc.) and just compare those. (Incidentally, you can use a letter as an array index in Perl, but I don't recommend it and it generates a warning.) You can end up turning text processing into Linear Algebra if you go far enough down that road.

Perl's map function can do some pretty crazy stuff, too. For example, the fairly well-known "Schwartzian transform" (See: http://en.wikipedia.org/wiki/Schwartzian_transform if you want more information). Someday, I intend to sit down and play with map until using it comes naturally to me. I've even programmed some Lisp (AutoLisp, to be exact), but I know that I'm still thinking in procedural terms and I'd like to understand functional programming properly someday, and applying a function to a list of values is pretty much all that you do in functional languages.

It can, sometimes and with great skill, be used to write understandable code.
Most of them (if not all) are just unpopular features.
I would have said 'little-known' or 'not as well-known as they deserve to be', but it's a fair point.

Many of them are not really hidden so much as underused. (As I recall all of the "Hidden features of language X" questions on SO tended to drift that way pretty quickly.

I'm depressed to say that I knew most of these. Can I have those braincells back?
Same here. Recalling my triumphs at Perl Golf somehow seems like small consolation now that the Perl world has collectively steered themselves into a ditch.