gasilcw.blogg.se

Awk permute array
Awk permute array




awk permute array

The "List::Util" is in the Perl core so you don't need to worry about installing it.Īs a result of auto-splitting the split fields end up in the " " array and the " sum " function just sums them up. perl -MList::Util=sum -alne 'print sum one-liner turns on field auto-splitting with " -a " command line option and imports the "sum" function from " List::Util" module with " -MList::Util=sum " option. Print the sum of all the fields on a line. The " -lne " command line options have been explained in parts one and two.Ģ2. It matches the original string, therefore the number is not a prime. The back-reference " \1 " becomes " 11 " and the regex becomes " ^11(11)+$ ". The " (11+?) " matches the first two ones " 11 ". The number 4 in unary representation is " 1111 ". This repeats for " 1111 " and " 11111 ", which also don't match, therefore the whole regex doesn't match and the number is a prime. The back-reference becomes " 111 " and the whole regex becomes " ^111(111)+$ ". But since it used " +? ", it backtracks and matches the first three ones " 111 ". It can't match five ones, therefore it fails. The back-reference " \1 " becomes " 11 " and the whole regex now becomes " ^11(11)+$ ". The " (11+?) " matches first two ones " 11 ". The number 5 in unary representation is " 11111 ". Let's look at the second regex part on numbers 5 and 6. If two or mores 1s repeatedly make up the whole number, the regex matches, which means that the number is composite. The second part determines if two or more 1s repeatedly make up the whole number. Clearly, empty string and 1 are not prime numbers, therefore this regular expression matches, which indicated that they are not prime numbers. The first part matches " 1 " and empty string. If it doesn't match, the number is a prime, otherwise it's a composite. Next, the unary number gets tested against the ingenious regular expression. For example, 5 gets converted into " 1x5 ", which is " 11111 ". I included it for its artistic value.įirst, the number is converted in its unary representation by " (1x$_) ". This one-liner uses an ingenious regular expression to detect if a given number is a prime or not.

awk permute array

Check it out:Īnd here are today's one-liners: CalculationsĢ1. Even tho it's mostly copied out of perldoc perlvar, it's still handy to have in front of you, so print it.Īwesome news: I have written an e-book based on this article series. A few years ago I compiled all the Perl special variables in a single file and called it Perl special variable cheat-sheet.

awk permute array

The one-liners will make heavy use of Perl special variables. Subscribe to my blog to know when that happens! Part IX: Release of Perl One-Liners e-book.Īfter I'm done explaining all these one-liners, I'll publish an ebook.Part VI: Selective printing and deleting of certain lines.Part V: Text conversion and substitution.

awk permute array

The article on Perl one-liners will consist of at least seven parts: Perl one-liners is my attempt to create " perl1line.txt" that is similar to " awk1line.txt" and " sed1line.txt" that have been so popular among Awk and Sed programmers. See part one for introduction of the series. In this part I will create various one-liners for calculations. This is the third part of a seven-part article on Perl one-liners.






Awk permute array