8 1 String Manipulation CGI/Perl Programming By Diane Zak
8 2 Objectives In this chapter, you will: Convert a string to uppercase letters using the uc function Convert a string to lowercase letters using the lc function Return, replace, or insert text using the substr function Use the index function to locate a string within another string
8 3 Objectives In this chapter, you will: Replace text using the transliteration operator Use the binding operators Perform pattern matching using the matching operator Include metacharacters in a search pattern Replace text using the substitution operator
8 4 Introduction Often, a script will need to manipulate data it receives from a server –Formatting –Conversion –Other tasks
8 5 The uc and lc functions String comparisons are case sensitive Instead of checking for multiple cases within a condition statement, can use functions to temporarily change the case
8 6 The uc and lc functions uc function: –Changes string to all upper case –Syntax: uc (string) –Example: if (uc($answer) eq “Y”) { statement(s) to process if condition is true } Results: Temporarily converts the contents of the $answer variable to uppercase, then compares the uppercase value to “Y”
8 7 The uc and lc functions lc function: –Changes string to all lower case –Syntax: lc (string) –Example: while (lc($item) ne “done”) { statement(s) to process while condition is true } Results: Temporarily converts the contents of the $item variable to lowercase, then compares the lowercase value to “done”
8 8 The uc and lc functions Both functions only affect alphabetic characters The parentheses around the variable name is optional Can also use ucfirst function –Converts only first character in string to uppercase –Can use with lc to capitalize first letter of string and have the remaining letters in lowercase
8 9 The substr function Can use substr function to: –Return a portion of a string within a string –Replace one or more characters in a string –Insert one or more characters within a string
8 10 The substr function Using substr to return characters: –Syntax: substr (string, start, length) –Return length number of characters from string beginning at start –If no length, return all characters from start to end of string –Examples: $name = “John Smitty”; $first = substr ($name, 0, 4); Assigns John to $first $name = “John Smitty”; $last = substr ($name, 5, 6); $last = substr ($name, 5); Both substr statements assign Smitty to $last
8 11 The substr function Using substr to replace characters: –Syntax: substr (string, start, length) = replacementstring –Replace length number of characters in string with replacementstring, beginning at start in string –If no length, replaces all characters from start to end of string –Examples: $name = “Pat Jeffrey”; substr ($name, 0, 3) = “Patty”; Replaces Pat with Patty. $name now stores Patty Jeffrey. $name = “Pat Jeffrey”; substr ($name, 4) = “Carr”; Replaces Jeffrey with Carr. $name now stores Pat Carr.
8 12 The substr function Using substr to insert characters: –Syntax: substr (string, start, 0) = insertionstring –Insert insertionstring within string, beginning at position start in string. –Examples: $price = “45”; substr ($price, 0, 0) = “\$”; Inserts a dollar sign in position 0 of $price variable, which now stores $45 $location = “Chicago IL”; substr ($location, 7, 0) = “, “; Inserts a comma in position 7 of $location variable, which now stores Chicago, IL.
8 13 The index function Can be used to search a string to determine if it contains another string Syntax: –index (string, substring, start) –Returns the position of the first occurrence of substring within the string beginning with start position in string. If substring is not found, function returns –1 Otherwise, returns the beginning position of the first occurrence of substring –If start is omitted, the search begins at the first character in string.
8 14 The index function
8 15 The Transliteration and “Contains” Operators Transliteration operator: –Also referred to as the tr operator or the tr/// operator –Can be used to replace a character in a string –Syntax: string =~ tr/searchlist/replacementlist/modifier –searchlist – character(s) to search for –replacementlist – optional. character(s) to replace character(s) listed in searchlist –modifier – optional. »dDelete found but unreplaced characters »cComplement the searchlist »sSquash duplicate replaced characters to a single character
8 16 The Transliteration and “Contains” Operators “contains” operator: –Also referred to as a binding operator –Represented by =~ –Used with tr/// operator Tells tr/// operator to search string to determine if it contains any of the characters in searchlist
8 17 The Transliteration and “Contains” Operators
8 18 The Transliteration and “Contains” Operators
8 19 The Match Operator Can be used to determine if a string is contained within another string –Like the index function –Referred to as the m operator or the m/// operator –Can use the “contains” operator (=~) or the “does not contain” operator (!~)
8 20 The Match Operator
8 21 Using Metacharacters in a Search Pattern
8 22 Using Metacharacters in a Search Pattern
8 23 Using Metacharacters in a Search Pattern Metacharacters and the m/// operator can be used to verify an address:
8 24 The Substitution Operator Can be used to: –Replace one or more characters in a string –Remove one or more characters from a string –Also referred to as the s operator or the s/// operator
8 25 The Substitution Operator
8 26 The International Coffees Form and Script
8 27 The International Coffees Form and Script
8 28 Summary The uc function temporarily converts the letters in a string to uppercase. The lc function temporarily converts the letters in a string to lowercase. You can use the substr function to: –Return portion of a string contained in another string –Replace character(s) in a string –Insert character(s) into a string
8 29 Summary The index function to determine if string contains substring –Returns position number of beginning of substring If substring is not within string, returns –1 The tr/// (transliteration) operator can replace a character in a string, or delete a character from a string. The m// (match) operator can determine if a string is contained in another string. –Returns true or false –If assigned to an array, returns list of values that match searchpattern
8 30 Summary The searchpattern entered in the m// and s/// operators can include metacharacters: –Match single character(s) –Match whitespace character(s) –Match boundary character(s) –Match repeated characters The s/// (substitution) operator can be used to replace or delete character(s) in a string.