Famous Perl One-Liners Explained, Part IV: String and Array Creation - good coders code, great reuse |
| Famous Perl One-Liners Explained, Part IV: String and Array Creation Posted: 06 Jan 2010 09:00 PM PST
Famous 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. The article on famous Perl one-liners will consist of nine parts:
I decided that there will be two new parts in this series. The most powerful feature in Perl is its regular expressions, therefore I will write a part on “Handy Perl regular expressions.” I also decided to publish an e-book after I am done with the series, so that will be the last part of this series. Everyone who’s subscribed to my blog will get a free copy! Subscribe to my blog now! I also updated the previous part on calculations with 14 new one-liners on finding values of constants pi and e, doing date calculations, finding factorial, greatest common divisor, least common multiple, generating random numbers, generating permutations, finding power sets and doing some IP address conversions. Here are today’s one-liners: String Creation and Array Creation49. Generate and print the alphabet. perl -le 'print a..z' This one-liner prints all the letters from a to z as I really golfed this one-liner. If you used perl -le 'print ("a".."z")' Remember that the range operator perl -le '$, = ","; print ("a".."z")' There are many more special variables. Take a look at my special variable cheat sheet for a complete listing. 50. Generate and print all the strings from “a” to “zz”. perl -le 'print ("a".."zz")' Here the range operator Similarly you may generate all strings from “aa” to “zz” by: perl -le 'print "aa".."zz"' Here it goes like “aa”, “ab”, …, “az”, “ba”, “bb”, …, “bz”, “ca”, … “zz”. 51. Create a hex lookup table. @hex = (0..9, "a".."f") Here the array You may use this array to convert a number (in variable perl -le '$num = 255; @hex = (0..9, "a".."f"); while ($num) { $s = $hex[($num%16)&15].$s; $num = int $num/16 } print $s' 52. Generate a random 8 character password. perl -le 'print map { ("a".."z")[rand 26] } 1..8' Here the map function executes If you also wish to include numbers in the password, add perl -le 'print map { ("a".."z", 0..9)[rand 36] } 1..8' If you need a longer password, change 53. Create a string of specific length. perl -le 'print "a"x50' Operator If the repetition operator is used in list context, it creates a list (instead of scalar) with the given elements repeated: perl -le '@list = (1,2)x20; print "@list"' This one liner creates a list of twenty repetitions of (1, 2) (it looks like (1, 2, 1, 2, 1, 2, …)). 54. Create an array from a string. @months = split ' ', "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec" Here the 55. Create a string from an array. @stuff = ("hello", 0..9, "world"); $string = join '-', @stuff Here the values in array 56. Find the numeric values for characters in the string. perl -le 'print join ", ", map { ord } split //, "hello world"' This one-liner takes the string “hello world”, splits it into a list of characters by Another way to do the same is use the perl -le 'print join ", ", unpack("C*", "hello world")' 57. Convert a list of numeric ASCII values into a string. perl -le '@ascii = (99, 111, 100, 105, 110, 103); print pack("C*", @ascii)' Just as we unpacked a string into a list of values with the Another way to do the same is use the perl -le '@ascii = (99, 111, 100, 105, 110, 103); print map { chr } @ascii' Similar to one-liner #55 above, function 58. Generate an array with odd numbers from 1 to 100. perl -le '@odd = grep {$_ % 2 == 1} 1..100; print "@odd"' This one-liner generates an array of odd numbers from 1 to 99 (as 1, 3, 5, 7, 9, 11, …, 99). It uses the 59. Generate an array with even numbers from 1 to 100. perl -le '@even = grep {$_ % 2 == 0} 1..100; print "@even"' This is almost the same as the previous one-liner, except the condition 60. Find the length of the string. perl -le 'print length "one-liners are great"' Just for completeness, the 61. Find the number of elements in an array. perl -le '@array = ("a".."z"); print scalar @array' Evaluating an array in a scalar context returns the number of elements in it. Have Fun!Have fun with these one-liners for now. The next part is going to be about text conversion and substitution. Can you think of other string creating and array creation one-liners that I didn’t include here? |
| You are subscribed to email updates from good coders code, great reuse To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
| Google Inc., 20 West Kinzie, Chicago IL USA 60610 | |
No comments:
Post a Comment
Keep a civil tongue.