Chapter 5:Perl One-Liners:Working with Arrays and Strings

In this chapter, we’ll look at various one-liners for

  • creating strings and arrays,
  • for doing things like generating passwords,
  • creating strings of certain length,
  • finding the numeric values of characters,
  • creating arrays of numbers.

You’ll also learn about the range operator .., the x operator, the $, special variable, and the @ARGV array.

5.1 Generate and print the alphabet
    perl -le 'print a..z'

The $, is the field separator. It’s output by print between each field. Semantically, though, using join to separate the list of letters with a comma is more appealing because it works even when not using print directly:

    perl -le '$alphabet = join ",", ("a".."z"); print $alphabet'
5.2 Generate and print all the strings from “a” to “zz”
5.3 Create a hex lookup table
perl -le '
$num = 255;
@hex = (0..9, "a".."f");
while ($num) {
$s = $hex[($num % 16)].$s;
$num = int $num/16;
}
print $s
'

But surely, converting a number to hex is much easier if I use printf(or sprintf) with the %x format specifier.

    perl -le 'printf("%x", 255)'

To convert the number back from hex to dec, use the hex operator:

    perl -le '$num = "ff"; print hex $num'
5.4 Generate a random eight-character password
        perl -le 'print map { ("a".."z")[rand 26] } 1..8'
5.5 Create a string of specific length
    perl -le 'print "a"x50'

For example, if you need 1KB of data, just
do this:

    perl -e 'print "a"x1024'

When you use the repetition operator in the list context, with a list as its first operand, you create a list with the given elements repeated, like this:

    perl -le '@list = (1,2)x20; print "@list"'
5.6 Create an array from a string
    @months = split ' ', "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
5.7 Create a string from the command-line arguments
    perl -le 'print "(", (join ",", @ARGV), ")"' val1 val2 val3
5.8 Find the numeric values for characters in a string
    perl -le 'print join ", ", map { ord } split //, "hello world"'

You could also do this with the unpack operator by specifying C* as the unpacking template:

    perl -le 'print join ", ", unpack("C*", "hello world")'
5.9 Convert a list of numeric ASCII values into a string
perl -le '
@ascii = (99, 111, 100, 105, 110, 103);
print pack("C*", @ascii)
'

You can also use the @ARGV array and pass the ASCII values as arguments to the one-liner:

    perl -le 'print map chr, @ARGV' 99 111 100 105 110 103
5.10 Generate an array with odd numbers from 1 to 100
  perl -le '@odd = grep {$_ % 2 == 1} 1..100; print "@odd"'
5.11 Generate an array with even numbers from 1 to 100
      perl -le '@even = grep {$_ % 2 == 0} 1..100; print "@even"'
5.12 Find the length of a string
    perl -le 'print length "one-liners are great"'
5.13 Find the number of elements in an array
    perl -le '@array = ("a".."z"); print scalar @array'
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容