Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 – String Manipulation

Similar presentations


Presentation on theme: "Chapter 9 – String Manipulation"— Presentation transcript:

1 Chapter 9 – String Manipulation
Outline 9.1 Introduction 9.2 Quotes and Quote Operators 9.3 “Here” Documents 9.4 Basic String Functions 9.5 chop and chomp Functions 9.6 index and rindex Functions 9.7 Function join 9.8 Function split 9.9 tr// Translation Operator 9.10 Formatting Outputs with printf and sprintf 9.11 Printing with Fields Width and Precision 9.12 Using Flags in the printf Format Control String 9.13 Evaluating Strings as Perl Code

2 1 #!/usr/bin/perl 2 # Fig. 9.1: fig09_01.pl 3 # Demonstrating properties of printing with single quotes. 4 5 use warnings; 6 use strict; 7 8 print 'I\'ve '; # single quotes interpret "\'" 9 print 'Had Better '; 10 print 'Days. \n'; # single quotes do not interpret "\n" 11 12 print "\n\n"; 13 14 # single quotes interpret "\\" 15 print 'Printing a backslash: \\'; 16 17 print "\n\n"; 18 19 print 20 'La di da 21 di da di'; Prints the word I've, showing that single quotes recognize the backslash-single-quote escape sequence. Other escape sequences print literally, as shown when Perl prints \n after the word Days. The only other escape sequence that is recognized is the backslash backslash escape sequence. A newline may be added by formatting a single-quoted string exactly as it should appear on the screen. I've Had Better Days. \n Printing a backslash: \ La di da di da di

3

4 1 #!/usr/bin/perl 2 # Fig. 9.3: fig09_03.pl 3 # Demonstration of here documents 4 5 use warnings; 6 use strict; 7 8 my $notInterpolated = "Interpolated!"; 9 10 print <<DONE; 11 For here documents the type of quotes determines 12 what kind of interpolation is done. No quotes means 13 double quoted interpolation. $notInterpolated 14 15 DONE 16 17 print <<'DONE'; 18 Single quotes do not allow interpolation. 19 $notInterpolated. 20 21 DONE 22 23 my $variable = <<'DONE'; 24 Here documents do not have to be used 25 with the print function. 26 27 DONE 28 If the identifier is unquoted or double quoted, the resulting string inherits the properties of a double-quoted string, including interpolation of escape sequences and variables If the identifier is single quoted, the string inherits the properties of a single-quoted string. Here documents do not have to be used with the print function. In this case, a here document is used to assign a multiline string to the scalar $variable.

5 29 print $variable; 30 31 ($variable = <<DONE) =~ s/^\s+//gm; All the leading spaces will be ignored so you can indent to your heart's content. 35 36 DONE 37 38 print $variable; For here documents the type of quotes determines what kind of interpolation is done. No quotes means double quoted interpolation. Interpolated! Single quotes do not allow interpolation. $notInterpolated. Here documents do not have to be used with the print function. All the leading spaces will be ignored so you can indent to your heart’s content.

6 Operator uc is used to make each letter in $string uppercase.
1 #!/usr/bin/perl 2 # Fig. 9.4: fig09_04.pl 3 # Demonstrating basic string functions. 4 5 use warnings; 6 use strict; 7 8 my $string = "hello there\n"; 9 print "The original string: ", 'hello there\n', "\n\n"; 10 11 # Using substr 12 print "Using substr with the string and the offset (2): "; 13 print substr( $string, 2 ); 14 print "Using substr with the string, offset (2) and length (3): "; 15 print substr( $string, 2, 3 ), "\n"; 16 print "Using substr with offset (-6), and length (2): "; 17 print substr( $string, -6, 2 ), "\n"; 18 print "Using substr with offset (-6) and length (-2): "; 19 print substr( $string, -6, -2 ), "\n\n"; 20 21 # replace first 5 characters of $string with "bye" 22 # assign substring that was replaced to $substring 23 my $substring = substr( $string, 0, 5, "Bye" ); 24 25 print "The string after the replacement: $string"; 26 print "The substring that was replaced: $substring\n\n"; 27 28 # convert all letters of $string1 to uppercase 29 $string = uc( $string ); 30 print "Uppercase: $string"; We begin by using substr with only the first two arguments. The string is $string, and the offset is 2 (i.e., skip the first two characters), returning everything from the third character to the end of $string. This call to substr uses an offset of -6 and a length of 2, printing two letters of this string, starting from the letter t, six characters from the end of the string. This call to substr uses the offset -6 and the length -2, thus printing all of the letters from the t six characters from the end of the string up to, but not including, the second-to-last character. The second call adds the length argument, specifying that the returned substring should skip the first two characters and pick three characters from the string; thus, the substring is llo. Operator uc is used to make each letter in $string uppercase. This call to substr uses all of the arguments (replacement is set to "bye") with substr and assigns the returned value to $substring.

7 Operator lc is used to make each letter in $string lowercase.
31 32 # convert all letters of $string to lowercase 33 $string = lc( $string ); 34 print "Lowercase: $string \n"; 35 36 # only change first letter to lowercase 37 $string = lcfirst( $string ); 38 print "First letter changed to lowercase: $string"; 39 40 # only change first letter to uppercase 41 $string = ucfirst( $string ); 42 print "First letter changed to uppercase: $string \n"; 43 44 # calculating the length of $string 45 my $length = length( 'Bye there\n' ); 46 print "The length of \$string without whitespace: $length \n"; 47 48 $length = length( "Bye there\n" ); 49 print "The length of \$string with whitespace: $length \n"; 50 51 $length = length( $string ); 52 print "The length of \$string (default) is: $length \n"; Operator lc is used to make each letter in $string lowercase. The lcfirst function returns its expression with the first character in lowercase. This call to length determines the length of the same string ("bye there\n"), but in double quotes. This case returns the value 10, because \n is interpolated as the newline character, rather than as the two characters that compose the escape sequence. The ucfirst function returns its expression with the first character in uppercase. The first call to length determines the length of the string ‘Bye there\n' in single quotes, resulting in the value 11 (remember that the \n is not interpolated here). The length function returns the length of its expression in characters. When the variable $string is passed as the argument to length, the string is interpolated as if it were enclosed in double quotes.

8 The original string: hello there\n
Using substr with the string and the offset (2): llo there Using substr with the string, offset (2) and length (3): llo Using substr with offset (-6), and length (2): th Using substr with offset (-6) and length (-2): ther The string after the replacement: Bye there The substring that was replaced: hello Uppercase: BYE THERE Lowercase: bye there First letter changed to lowercase: bye there First letter changed to uppercase: Bye there The length of $string without whitespace: 11 The length of $string with whitespace: 10 The length of $string (default) is: 10

9 1 #!/usr/bin/perl 2 # Fig. 9.5: fig09_05.pl 3 # Using the chop and chomp functions 4 5 use warnings; 6 use strict; 7 8 print "Input something and we'll print it: "; 9 my $string = <STDIN>; 10 11 print "\nOur input ( $string ) contains a newline. Remove it:\n"; 12 chop( $string ); 13 14 print "\nThis is more like it: '$string' without the newline.\n"; 15 16 # Removing the last character, regardless of what it is 17 my $character = chop( $string ); 18 print "\nOops! We removed $character; now it is '$string.'\n"; 19 20 chomp( $string ); # Last character removed only if a newline 21 print "\nAhh. This is safer. Still '$string.'\n"; 22 23 = ( "One\n", "Two\n", "Three", "Four", "Five\n" ); 24 25 # We can apply chomp to all elements of an array 26 my $newlines = ); 27 print "\nWe just removed $newlines newlines from that list!\n"; 28 print "\nThe list is The chop function removes the last character from a string and returns that character. In this case, the newline character is removed from the end of the string. When the function is called again, it causes another character to be removed from the end of the input. Running chomp on the current string results in no action, because chomp removes the last character only if it is a newline. The last call to chomp is performed on an array. When this call occurs, chomp operates on all of the elements of the array.

10 Input something and we'll print it: Hi there
Our input (Hi there ) contains a newline. Remove it: This is more like it: 'Hi there' without the newline. Oops! We removed e; now it is 'Hi ther.' Ahh. This is safer. Still 'Hi ther.' We just removed 3 newlines from that list! The list is now One Two Three Four Five.

11 1 #!/usr/bin/perl 2 # Fig. 9.6: fig09_06.pl 3 # Demonstrating special properties of chomp. 4 5 my $string1 = "This is a string"; 6 print "\$string1 is \"$string1\""; 7 print "\n\nChanging \$/ to \"g\" \n\n"; 8 9 $/ = "g"; 10 11 # Removing last character if it is equal to "g" 12 chomp( $string1 ); 13 print "The new \$string1 is \"$string1\"\n"; 14 15 my $string2 = "This is a string with three newlines\n\n\n"; 16 17 print "\n\$string2 now is \"$string2\" <-- Where \$string2 ends."; 18 19 print "\n\nChanging \$/ to \"\" \n\n"; 20 21 $/ = ""; 22 23 # All trailing newlines are removed 24 my $numberNewlines = chomp( $string2 ); 25 26 print "\$string2 was called with chomp.\n"; 27 print "It now has no newlines: \"$string2\""; 28 29 # chomp returns the number of characters removed 30 print "\nchomp returned the number $numberNewlines.\n"; When we make the special variable $/ equal to “g” and call chomp on $string1, the last character is removed. Changing $/ to the empty string causes chomp to remove all of the trailing newlines.

12 $string1 is "This is a string"
Changing $/ to "g" The new $string1 is "This is a strin" $string2 is "This is a string with three newlines " <-- Where $string2 ends. Changing $/ to "" $string2 was called with chomp. It now has no newlines: "This is a string with three newlines" chomp returned the number 3.

13 $label is incremented after placing its value in the hash.
1 #!/usr/bin/perl 2 # Fig. 9.7: fig09_07.pl 3 # Demonstration of the index function 4 5 use warnings; 6 use strict; 7 8 my $string = "babaabababbaababbababababaaabblp"; 9 print "We're going to number each bab.\n"; 10 print $string, "\n"; 11 12 my $foundAt = 0; 13 my $offset = 0; 14 my $label = 1; 15 my %positions; 16 17 while ( ( $foundAt = index( $string, 'bab', $offset ) ) > -1 ) { 18 $positions{ $foundAt } = $label++; 19 $offset = $foundAt + 1; 20 } 21 22 foreach ( 0 .. length( $string ) - 1 ) { 23 print $positions{ $_ } ? $positions{ $_ } : " "; 24 } Each time bab is found, the location is assigned to variable $foundAt, which is compared with -1. If $foundAt is greater than -1, an entry is added to hash %positions, using $foundAt as the key. When all occurrences have been found, function index returns -1, and the while loop terminates. The while condition at searches for bab in $string, starting from the location $offset (which is initialized to zero). Function index returns the position of the first occurrence of a given substring within a given string. We then iterate over the length of the string, printing out the label if a bab was found at that index, and a space otherwise. $offset is adjusted so that the next search will begin one character after the last-found occurrence. $label is incremented after placing its value in the hash. We're going to number each bab. babaabababbaababbababababaaabblp

14 1 #!/usr/bin/perl 2 # Fig. 9.8: fig09_08.pl 3 # The join function 4 5 use warnings; 6 use strict; 7 8 my $line, $string ); 9 10 print "Input several words on separate "; 11 print "lines, typing \"done\" when finished:\n\n"; 12 13 while ( $line = <STDIN> ) { 14 chomp $line; 15 $line eq 'done' ? last : push $line ); 16 } 17 18 $string = join( ', ); 19 print "\n$string"; The while loop iterates until the user enters done. For each iteration, the input gets pushed The join function combines the individual strings in a given list into one string. Each input is joined into one scalar $string, separating each input by the separator ',' .

15 Input several words on separate lines, typing "done" when finished:
Now I am one string done Now, I, am, one, string

16 The split function scans a string and splits it into separate fields.
1 #!/usr/bin/perl 2 # Fig. 9.9: fig09_09.pl 3 # Demonstrates the split function 4 5 use warnings; 6 use strict; 7 8 my $string = "I will be divided up."; 9 = split( / /, $string ); 10 print "$_\n" foreach ); 11 12 # Divide it into a specific number of fields. = split( / /, $string, 3 ); 14 print "\n"; 15 print "$_\n" foreach ); 16 17 # Divide according to multiple separators 18 $string = = split( $string ); 20 print "$_\n" foreach ); 21 22 # Keeping the separators (by having them in parentheses) 23 $string = join( ',', split( $string ) ); 24 print "$string\n\n"; 25 26 # Defaults to split(' ', $_) 27 $_ = "Separated by whitespace\n"; 28 print "$_\n" foreach ( split ); The split function scans a string and splits it into separate fields. Splits on the space character. Also splits on the space character, but only splits the string three ways (because it is given a limit argument of 3). A character class of delimiters is given on which to split. This line splits the $string, but keeps the separators. The separators are considered as fields and stored as such. By using parentheses in the matching expression, the split function maintains the enclosed separators. These strings are joined together with delimiting commas by using the join function.

17 I will be divided up. be divided up. up, too. Separated by whitespace

18 The results of the translation are printed.
1 #!/usr/bin/perl 2 # Fig. 9.10: fig09_10.pl 3 # Introducing the translation operator 4 5 use warnings; 6 use strict; 7 8 my $string = "lots of letters"; 9 $_ = "hello, cuz"; 10 11 print "$string\n"; 12 print "$_\n\n"; 13 14 $string =~ 15 16 17 print "$string\n"; 18 print "$_\n"; The letters a to z, specified after the first /, are translated into the characters following the next /. The translation operator (tr///) substitutes specific characters for ones found in a given string. If the binding operator is not used, the operator searches and modifies variable $_. The results of the translation are printed.

19

20

21 Displays signed decimal integers. Displays an unsigned octal integer.
1 #!/usr/bin/perl 2 # Fig. 9.13: fig09_13.pl 3 # Using the integer conversion specifiers 4 5 use warnings; 6 use strict; 7 8 printf "%d\n", ; 9 printf "%d\n", ; 10 printf "%d\n", -455; 11 printf "%o\n", 455; 12 printf "%u\n", 455; 13 printf "%u\n", -455; 14 printf "%x\n", 455; 15 printf "%x\n", -455; Every printf call contains a format-control string that describes the format of the output. The format-control string consists of literal characters, conversion specifiers and flags. Formatted output allows a programmer to specify output lengths and types, forcing output expressions to be converted into a specific format. The printf and sprintf functions create precisely formatted outputs. Displays signed decimal integers. Displays an unsigned octal integer. Displays unsigned decimal integers. Displays unsigned hexadecimal integers. 455 -455 707 1c7 fffffe39

22 Displays a character given an ASCII value.
1 #!/usr/bin/perl 2 # Fig. 9.14: fig09_14.pl 3 # Printing formatted characters and strings 4 5 use warnings; 6 use strict; 7 8 my $string = "I am a string"; 9 = ( "An", 'array', "of strings" ); 10 11 printf "%c is ASCII value 65 and %c is value 66\n", 65, 66; 12 printf "The string is \"%s\"\n", $string; 13 printf "This string is %s\n", "literal"; 14 printf "%s %s %s %s 15 printf "%d%%\n", 45; # printing a percent sign after 45 Displays a string. Displays a character given an ASCII value. The format-control-string can contain additional conversion specifiers. Because there are only three fields the function ignores the extra %s specifiers. Displays a percent sign. A is ASCII value 65 and B is value 66 The string is "I am a string" This string is literal An array of strings 45%

23 1 #!/usr/bin/perl 2 # Fig. 9.15: fig09_15.pl 3 # Using sprintf 4 5 use warnings; 6 use strict; 7 8 my $product = "sweater"; 9 my $price = 39; 10 11 my $line = sprintf "The %s costs \$%d\n.", $product, $price; 12 print $line; Function sprintf is identical to printf, except that it returns, rather than prints, the formatted string. sprintf enables you to maintain a formatted string within your program. The sweater costs $39.

24 1 #!/usr/bin/perl 2 # Fig. 9.16: fig09_16.pl 3 # Printing integers right-justified 4 5 use warnings; 6 use strict; 7 8 printf "%4d\n", 1; 9 printf "%4d\n", 12; 10 printf "%4d\n", 123; 11 printf "%4d\n", 1234; 12 printf "%4d\n", 12345; 13 printf "%4d\n\n", ; 14 15 printf "%4d\n", -1; 16 printf "%4d\n", -12; 17 printf "%4d\n", -123; 18 printf "%4d\n", -1234; 19 printf "%4d\n", ; 20 printf "%4d\n", ; The exact size of a field in which data is printed is specified by a field width. If the field width is larger than the length of the data being printed, the data normally is right justified in that field and preceded by leading blanks. To format data in a field, an integer representing the field width is inserted between the percent sign (%) and the conversion specifier in the conversion specification.

25 1 12 123 1234 12345 -1 -12 -123 -1234 -12345

26 1 #!/usr/bin/perl 2 # Fig. 9.17: fig09_17.pl 3 # Using precision while printing integers, 4 # floating-point numbers, and strings 5 6 use warnings; 7 use strict; 8 9 my $integer = 873; 10 my $float = ; 11 my $string = "Happy Birthday"; 12 13 printf "Using precision for integers\n"; 14 printf "\t%.2d\n\t%.4d\n\t%.9d\n\n", $integer, $integer, $integer; 15 16 printf "Using precision for floating-point numbers\n"; 17 printf "\t%.3f\n\t%.3e\n\t%.3g\n\n", $float, $float, $float; 18 19 printf "Using precision for strings\n"; 20 printf "\t%.11s\n", $string; When used with integer conversion specifiers, precision indicates the minimum number of digits to be printed. If the printed value contains fewer digits than the precision value, zeros are prefixed to the printed value until the total number of digits is equivalent to the precision value. To use precision, place a decimal point (.) followed by an integer representing the precision between the percent sign and the conversion specifier. When used with floating-point conversion specifiers e, E and f, the precision is the number of digits to appear after the decimal point. When used with conversion specifiers g and G, the precision is the maximum number of significant digits to be printed. When used with conversion specifier s, the precision is the maximum number of characters to be written from the string.

27 Using precision for integers
873 0873 Using precision for floating-point numbers 1.239e+002 124 Using precision for strings Happy Birth

28

29 1 #!/usr/bin/perl 2 # Fig. 9.19: fig09_19.pl 3 # Printing numbers with and without the + flag 4 5 use warnings; 6 use strict; 7 8 printf "%d\n%d\n", 786, -786; 9 printf "%+d\n%+d\n", 786, -786; Prints a positive number and a negative number, each with and without the + flag. Note that the minus sign is displayed in both cases, but the plus sign is displayed only when the + flag is used. 786 -786 +786

30 1 #!/usr/bin/perl 2 # Fig. 9.20: fig09_20.pl 3 # Printing a space before signed values 4 # not preceded by + or - 5 6 use warnings; 7 use strict; 8 9 printf "% d\n% d\n", 547, -547; Uses the space flag to prefix a space to positive numbers. This flag is useful for aligning positive and negative numbers with the same number of digits. 547 -547

31 1 #!/usr/bin/perl 2 # Fig. 9.21: fig09_21.pl 3 # Using the # flag with conversion specifiers 4 # o, x, X, and any floating-point specifier 5 6 use warnings; 7 use strict; 8 9 my $integer = 1427; 10 my $float = ; 11 12 printf( "%#o\n", $integer ); 13 printf( "%#x\n", $integer ); 14 printf( "%#X\n", $integer ); 15 printf( "\n%g\n", $float ); 16 printf( "%#g\n", $float ); Uses the # flag to prefix 0 to the octal value, 0x and 0X to the hexadecimal values and to force a decimal point to appear in a value printed with g. 02623 0x593 0X593 1427

32 Prints 452 again using only the 0 flag and a 9-space field.
1 #!usr/bin/perl 2 # Fig. 9.22: fig09_22.pl 3 # Printing with the 0 (zero) flag fills in leading zeros 4 5 use warnings; 6 use strict; 7 8 printf( "%+09d\n", 452 ); 9 printf( "%09d", 452 ); Combines the + flag and the 0 (zero) flag to print 452 in a 9-space field with a + sign and leading zeros. Prints 452 again using only the 0 flag and a 9-space field.

33 1 #!usr/bin/perl 2 # Fig. 9.23: fig09_23.pl 3 # Capturing fatal errors 4 5 use warnings; 6 use strict; 7 8 my $line = "25 / 0"; 9 10 eval ( $line; ) 11 12 print "There is an error:\n" if 13 print 14 print "Without eval, a fatal error would have ended the program."; The eval function returns the value of the last evaluated expression. If a syntax error or other fatal error exists in the block, eval returns undef and stores the error message in the special variable The eval function takes a single argument (either a string or a block) and evaluates it as Perl code in the body of a program. There is an error: Illegal division by zero at (eval 1) line 2. Without eval, a fatal error would have ended the program.

34 1 #!usr/bin/perl 2 # Fig. 9.24: fig09_24.pl 3 # Using /e modifier to evaluate 4 5 use warnings; 6 use strict; 7 8 my $string = "Let us convert a word to uppercase."; 9 10 print "$string\n"; 11 $string =~ s/(\bw\w+\b)/uc($1)/e; 12 print "$string\n"; Evaluation in the style of eval also can be embedded in the substitution operator. The /e modifier evaluates the replacement portion of the substitution operator, instead of treating it merely as a string. The /e modifier causes this function to be evaluated after $1 is interpolated, replacing the found text with an uppercase version of the text. The regular expression matches a word (surrounded by word boundaries \b) beginning with w and containing one or more additional letters (\w+). The surrounding parentheses cause any matching word to be assigned to $1. Variable $1 is used as the argument to the function uc in the replacement portion of the substitution operator. Let us convert a word to uppercase. Let us convert a WORD to uppercase.


Download ppt "Chapter 9 – String Manipulation"

Similar presentations


Ads by Google