Hi all! I am starting yet another article series here. They have received more than , views total now and they attract several thousand new visitors every week. Inspired by their success, I am going to create my own perl1line.

Author: | Mikatilar Dat |

Country: | Turkmenistan |

Language: | English (Spanish) |

Genre: | Technology |

Published (Last): | 5 January 2010 |

Pages: | 454 |

PDF File Size: | 13.57 Mb |

ePub File Size: | 1.9 Mb |

ISBN: | 255-3-94307-506-5 |

Downloads: | 66748 |

Price: | Free* [*Free Regsitration Required] |

Uploader: | Moogusho |

This is the third part of a seven-part article on Perl one-liners. In this part I will create various one-liners for calculations. See part one for introduction of the series. Perl one-liners is my attempt to create " perl1line.

After I'm done explaining all these one-liners, I'll publish an ebook. Subscribe to my blog to know when that happens! The one-liners will make heavy use of Perl special variables. A few years ago I compiled all the Perl special variables in a single file and called it Perl special variable cheat-sheet.

Even tho it's mostly copied out of perldoc perlvar , it's still handy to have in front of you, so print it. Awesome news: I have written an e-book based on this article series. Check it out:. This one-liner uses an ingenious regular expression to detect if a given number is a prime or not. Don't take it too seriously, though. I included it for its artistic value. For example, 5 gets converted into " 1x5 ", which is " ". Next, the unary number gets tested against the ingenious regular expression.

If it doesn't match , the number is a prime, otherwise it's a composite. The regular expression works this way. The first part matches " 1 " and empty string. Clearly, empty string and 1 are not prime numbers, therefore this regular expression matches, which indicated that they are not prime numbers.

The second part determines if two or more 1s repeatedly make up the whole number. If two or mores 1s repeatedly make up the whole number, the regex matches, which means that the number is composite. Otherwise it's a prime. The number 5 in unary representation is " ". It can't match five ones, therefore it fails. It doesn't match again. This repeats for " " and " ", which also don't match, therefore the whole regex doesn't match and the number is a prime. The number 4 in unary representation is " ".

It matches the original string, therefore the number is not a prime. The " -lne " command line options have been explained in parts one and two. The "List::Util" is in the Perl core so you don't need to worry about installing it. As a result of auto-splitting the split fields end up in the " F " array and the " sum " function just sums them up.

This one-liner keeps pushing the split fields in " F " to the " S " array. This sum is the sum of all fields over all lines. This solution isn't too good - it creates a massive array S. A better solution is to keep just the running:. This is almost the same as one-liner 22 above. Instead of summing all fields, it shuffles and prints them.

This is a tricky way to execute code inside quotes. It was needed to get the values of shuffled F separated by a space when printing them out. This one-liner uses the "min" function from "List::Util".

It's similar to all the previous ones. After the line has been automatically split by " -a ", the "min" function finds minimum element and prints it. It appends the contents of F to the array M. This one-liner stores all the data in memory. If you run it on a 10 terabyte file, it will die. Therefore it's better to keep the running minimum element in memory and print it out at the end:. Once the input ends, it prints the running minimum, which is the smallest value over all input.

This one-liner auto-splits the line by " -a " command line option. The split fields, as I already explained, end up in the F variable. Next it calls the absolute value function "abs" on each field by the help of "map" function. Finally it prints it joins all the fields by the help of array interpolation in double quotes. This one-liner forces to evaluate the F in scalar context, which in Perl means "the number of elements in F.

The result is number of words on all lines. This one-liner uses the " map " function that applies some operation on each of the elements in F array. Grep in scalar context returns the number of matches. The bignum package exports bpi function that calculates constant PI to wanted accuracy.

This one-liner prints PI to 20 decimal places. The bignum library also exports bexp function that takes two arguments - the power to raise e to and accuracy. This one-liner prints the constant e to 20 decimal places. The "gmtime" function is a Perl built-in function. If used in scalar context, it prints the time localized to Greenwich time zone. The "localtime" built-in function acts the same way as "gmtime", except it prints the computer's local time.

In array context both "gmtime" and "localtime" return a 9 element list struct tm with the following elements. You may slice this list, or print individual elements if you need just some part of this information. Remember that localtime returns a 9-list see above of various date elements.

The 4th element in the list is current month's day. If we subtract one from it we get yesterday. The "mktime" function constructs a Unix epoch time from this modified 9-list.

And "scalar localtime" construct prints out the new date, which is yesterday. It's supposed to normalize negative values. This one-liner modifies 0th, 4th, and 7th elements of now list. The 0th is seconds, the 4th is months and 7th is days see the table of 9 element time list above. Next, mktime creates Unix time from this new structure, and localtime, evaluated in scalar context, prints out the date that was 14 months, 9 days and 7 seconds ago.

This one-liner uses bfac function from Math::BigInt module that is in the Perl core no need to install. Change 5 to any number you want to find factorial for the value you are interested in. The result is 1 2 3 4 5, which is the factorial of 5. Math::BigInt has several other useful math functions. One of them is bgcd that calculates the greatest common divisor of a list of numbers. Surely, you can also use Euclid's algorithm.

Another function from Math::BigInt is lcm - the least common multiplicator. This one-liner finds lcm of 35, 20, 8 :. If you know some number theory, then you'll recall that there is a connection between gcd and lcm.

If we didn't set it to a space, the numbers would get printed concatenated together. This one-liner uses the object-oriented interface of Algorithm::Permute module to find the permutations all ways to rearrange items. The constructor of Algorithm::Permute takes an array reference to an array of elements to permute.

In this particular one-liner the elements are numbers 1, 2, 3, 4, 5. The next object function returns the next permutation. Calling it repeatedly iterates over all permutations. Please note that the output list gets large really quickly. There are n! Another way to print out all permutations is to use the exported permute subroutine:. It exports the powerset function, which takes a list of elements and returns a reference to a list containing references to subset lists.

Next I dereference the return value of powerset , which is a reference to a list of subsets. This one-liner converts the IP address Doing a for loop over a global match iterates over all the matches. These matches are the four parts of the IP address. But this one-liner doesn't do any error checking on the format of an IP address. This one-liner utilizes the fact that

ERICH SEGAL PRIZES PDF

## More Perl One-liners

This is the third part of a seven-part article on Perl one-liners. In this part I will create various one-liners for calculations. See part one for introduction of the series. Perl one-liners is my attempt to create " perl1line. After I'm done explaining all these one-liners, I'll publish an ebook. Subscribe to my blog to know when that happens! The one-liners will make heavy use of Perl special variables.

LA FIVETE PDF

## How to Make Perl Regex One-Liners

This is the introduction to perl one-liners. Originally I wrote this introduction for my third e-book , however later I decided to make it a part of the free e-book preview and republish it here as this article. Perl one-liners are small and awesome Perl programs that fit in a single line of code and they do one thing really well. These things include changing line spacing, numbering lines, doing calculations, converting and substituting text, deleting and printing certain lines, parsing logs, editing files in-place, doing statistics, carrying out system administration tasks, updating a bunch of files at once, and many more.

LETTERA A DIOGNETO TESTO PDF

## Perl one-liners

This is the second part of a seven-part article on Perl one-liners. In this part I will create various one-liners for line numbering. See part one for introduction of the series. Perl one-liners is my attempt to create " perl1line. The one-liners will make heavy use of Perl special variables.