8 1 String Manipulation CGI/Perl Programming By Diane Zak.

Slides:



Advertisements
Similar presentations
FORM VALIDATION Faheem Ahmed Khokhar. FORM VALIDATION Faheem Ahmed Khokhar.
Advertisements

Computer Science & Engineering 2111 Text Functions 1CSE 2111 Lecture-Text Functions.
Java Programming Strings Chapter 7.
1 Perl Syntax: substitution s// and character replacement tr//
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Programming and Perl for Bioinformatics Part I. A Taste of Perl: print a message perltaste.pl: Greet the entire world. #!/usr/bin/perl #greet the entire.
An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
JavaScript, Third Edition
1.
Chapter 8: String Manipulation
Programming with Microsoft Visual Basic th Edition
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
Chapter 06: Lecture Notes (CSIT 104) 1 Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 1 Copyright © 2008 Prentice-Hall. All rights reserved.
Chapter 9 Creating Formulas that Manipulate Text Microsoft Office Excel 2003.
Last Updated March 2006 Slide 1 Regular Expressions.
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL.
Chapter 8 Cookies And Security JavaScript, Third Edition.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
Copyright © 2008 Pearson Prentice Hall. All rights reserved Chapter 6 Data Tables and Amortization Tables Exploring Microsoft Office Excel 2007.
Oracle 11g: SQL Chapter 10 Selected Single-Row Functions.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
Chapter 8: Manipulating Strings
3 1 Sending Data Using an Online Form CGI/Perl Programming By Diane Zak.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
Chapter 23 The String Section (String Manipulation) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Tutorial 8: Manipulating Strings1 Tutorial 8 Manipulating Strings.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Strings in Python String Methods. String methods You do not have to include the string library to use these! Since strings are objects, you use the dot.
String and Lists Dr. José M. Reyes Álamo.
Microsoft Visual Basic 2008: Reloaded Third Edition
Logical Functions Excel Lesson 10.
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
Lecture 19 Strings and Regular Expressions
Strings, StringBuilder, and Character
The Selection Structure
Chapter 19 PHP Part II Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
Analyzing Data Using Formulas
Chapter 12: Text Processing
Chapter 12: Text Processing
Chapter 7: Strings and Characters
CIS16 Application Development and Programming using Visual Basic.net
String and Lists Dr. José M. Reyes Álamo.
Basic String Operations
Microsoft Visual Basic 2005: Reloaded Second Edition
Topics Basic String Operations String Slicing
An Introduction to Programming with C++ Fifth Edition
Introduction to Computer Science
The Selection Structure
Topics Basic String Operations String Slicing
Chapter 5: The Selection Structure
Topics Basic String Operations String Slicing
Presentation transcript:

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.