3 ms·
I like Krumins's explanations series even more than the bare one-liners: http://www.catonmat.net/search?q=Perl+one-liners+explained http://www.catonmat.net/sea
by JadeNB 10y ago
I like Krumins's explanations series even more than the bare one-liners: http://www.catonmat.net/search?q=Perl+one-liners+explained http://www.catonmat.net/search?q=Perl+one-liners+explained . (That said, a good context-free Perl one-liner can always be enjoyed on its mystifying own.) Note that there is a book with the one-liners (and, I assume, their explanations): https://www.nostarch.com/perloneliners https://www.nostarch.com/perloneliners .
He also posted about Abigail's classic primality-testing one-liner at http://www.catonmat.net/blog/perl-regex-that-matches-prime-numbers http://www.catonmat.net/blog/perl-regex-that-matches-prime-n... .
- waynecochran 10y agoFor real? # Check if a number is a prime perl -lne '(1x$_) !~ /^1?$|^(11+?)\1+$/ && print "$_ is prime"'
- JadeNB 10y agoI guess you mean "For real?" as in "Is this really a primality test?" Yes, it is, and Krumins describes how it works in the post that I linked (http://www.catonmat.net/blog/perl-regex-that-matches-prime-numbers http://www.catonmat.net/blog/perl-regex-that-matches-prime-n...). Essentially, although it looks complicated, it's the simplest possible idea: it's just trying factorisations, with the `(11+?)` part being the factor, and the `\1+` part insisting that we repeat that factor. It is of course wildly inefficient, and limitations in the backtracking engine mean that it can give false positives for large numbers (http://zmievski.org/2010/08/the-prime-that-wasnt http://zmievski.org/2010/08/the-prime-that-wasnt, also linked from that post).
- labster 10y agoI prefer the Perl 6 version, it's self-documenting: perl6 -n -e 'say "$_ is prime" if (+$_).is-prime'
- wyclif 10y agoThe Perl One-Liners book is great. I learned so much from it. You don't even need to have done any real Perl programming to get a lot out of that book, but if you want to learn Perl it will help you as well.