Perl, lesson 8
Text processing is what Perl was built for. It has a solid set of string functions, and regular expressions are part of the language itself.
reverse needs a word of explanation. In list context it reverses a list; in scalar
context it reverses a string. print gives its arguments list context, so you write
scalar reverse($s) to get the string version. Assigning to a scalar does the same:
my $r = reverse $s;.
split cuts a string into a list, join glues a list into a string:
The special pattern ' ' splits on any run of whitespace and skips leading spaces, which is
almost always what you want for words.
2 exercises to try what you just read.
A regular expression (regex) describes a pattern of text. Use =~ to apply one to a string:
\w is a word character, \d a digit, \s whitespace, + means "one or more", ^ and
$ anchor the start and end, and parentheses capture pieces into $1, $2, … .
Substitution replaces text. The g flag replaces every match, and r returns a modified
copy instead of changing the original:
tr counts or swaps characters: my $n = ($s =~ tr/a-z//); counts lowercase letters.
Remember from lesson 4: compare strings with eq, ne, lt and gt, and sort them with
cmp. For a case-insensitive comparison, compare lc($x) eq lc($y).
printf and sprintf pad and align: %-8s left-aligns a string in 8 columns, %5d
right-aligns a number in 5:
The last exercise is a small project using everything in this course: reading input, splitting lines, decisions, loops, a subroutine, arrays and formatted output. A good plan:
{ name => "Ada", score => 95 }, read back with $s->{name}.Read one line of text and print four lines:
For the palindrome test, ignore case and ignore every character that is not a letter. So
Never odd or even counts as a palindrome.
Sample input:
Sample output:
Time for a small project. The first input line is the number of students (at least 1). Each following line has a student's name and a whole-number score from 0 to 100, separated by a space.
Grades: 90 or more is A, 80 or more is B, 70 or more is C, 60 or more is D, anything
lower is F.
Print one line per student in input order, <name>: <score> (<grade>). Then print the
average score with one decimal place, and the name of the top student. If several students
share the top score, print the one who appears first.
Sample input:
Sample output:
my $s = "Hello, Perl";
print length($s), "\n"; # 11
print uc($s), "\n"; # HELLO, PERL
print lc($s), "\n"; # hello, perl
print ucfirst("perl"), "\n"; # Perl
print substr($s, 7), "\n"; # Perl (from position 7; positions start at 0)
print substr($s, 0, 5), "\n"; # Hello (5 characters from position 0)
print index($s, "l"), "\n"; # 2 (-1 when not found)
print "ab" x 3, "\n"; # ababab
print scalar reverse($s), "\n"; # lreP ,olleHmy @words = split ' ', " red green blue "; # (red, green, blue)
my @parts = split /,/, "a,b,c"; # (a, b, c)
print join("-", @words), "\n"; # red-green-bluemy $line = "milk 2";
if ($line =~ /^(\w+)\s+(\d+)$/) {
print "item=$1 qty=$2\n"; # item=milk qty=2
}my $code = "R2-D2";
(my $digits = $code) =~ s/\D//g; # "22" (\D = not a digit)
my $loud = $code =~ s/-/ /gr; # "R2 D2", $code unchangedprintf "%-8s|%5d|\n", "Ada", 95; # Ada | 95|Upper: <the line in uppercase>
Reversed: <the line reversed>
Length: <number of characters in the line>
Palindrome: <yes or no>Never odd or evenUpper: NEVER ODD OR EVEN
Reversed: neve ro ddo reveN
Length: 17
Palindrome: yes3
Ada 95
Ben 72
Cleo 88Ada: 95 (A)
Ben: 72 (C)
Cleo: 88 (B)
Average: 85.0
Top: Ada