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'
相關文章
- Chapter 1 Arrays and Strings - 1.7APT
- Effective C#-Working with Strings (翻譯) (轉)C#
- chapter5APT
- Chapter 5 查詢APT
- 學習perl(5)
- SG_007_CHAPTER 5APT
- use vs require in Perl5UI
- Arrays
- [譯][Perl 6] 5to6-perlfunc
- [譯][Perl 6] 5to6-nutshell
- Perl5的包和模組
- C++ Primer 5th筆記(5)chapter5 語句C++筆記APT
- delphi open arrays和dynamic arrays區別
- Working with State
- 六,Arrays
- Bash One-Liners Explained (二):操作字串AI字串
- Chapter5:使用Gradle管理多模組構建APTGradle
- working with ASH and AWR
- 【java】Arrays類Java
- Arrays工具類
- Chapter 1APT
- Working with Errors in Go 1.13ErrorGo
- Working with Matplotlib on OSX
- Java中Arrays作用Java
- Java Arrays.sort()Java
- java Arrays陣列Java陣列
- 18_Arrays類
- Bash One-Liners Explained(一):檔案處理AI
- 5,輸入與輸出(perl語言筆記)筆記
- perl
- Chapter 4 圖APT
- Chapter 6 排序APT排序
- chapter4APT
- chapter7APT
- chapter14APT
- chapter16APT
- RxSwift (二) Working with SubjectsSwift
- Working With Node.js and RedisNode.jsRedis