Home arrow Perl Programming arrow Page 7 - String Processing with Perl

On The Case - Perl

Perl has always been known for its text processing and manipulation abilities. This article examines the Perl string handling API in greater detail, explaining how you can use Perl's string functions to (among other things) print and format strings, split and join string values, alter string case, and perform regular expression searches.

TABLE OF CONTENTS:
  1. String Processing with Perl
  2. Jumping Jacks
  3. Choppy Waters
  4. Making New Friends
  5. Not My Type
  6. Of Jumping Cows And Purple Pumpkins
  7. On The Case
  8. Desperately Seeking Susan
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 76
April 16, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
The next few string functions come in very handy when adjusting the case of a text string from lower- to upper-case, or vice-versa:

lc() - convert string to lower case

uc() - convert string to upper case

ucfirst() - convert the first character of string to upper case

lcfirst() - convert the first character of a string to lower case

Here's an example:


#!/usr/bin/perl $str = "Something's rotten in the state of Denmark"; # returns "something's rotten in the state of denmark" print lc($str); # returns "SOMETHING'S ROTTEN IN THE STATE OF DENMARK" print uc($str); # returns "something's rotten in the state of Denmark" print lcfirst($str); # re-initialize for next bit of code $str = "something's rotten in the state of Denmark"; # returns "Something's rotten in the state of Denmark" print ucfirst($str);
You've already used the print() function extensively to display output. However, the print() function doesn't allow you to format output in any significant manner - for example, you can't write 1000 as 1,000 or 1 as 00001. And so clever Perl developers came up with the sprintf() function, which allows you to define the format in which data is printed.

Consider the following example:

#!/usr/bin/perl # returns 1.6666666666667 print(5/3);
As you might imagine, that's not very friendly. Ideally, you'd like to display just the "significant digits" of the result. And so, you'd use the sprintf() function:

#!/usr/bin/perl # returns 1.67 print sprintf("%1.2f", (5/3));
A quick word of explanation here: the Perl's sprintf() function is very similar to the printf() function that C programmers are used to. In order to format the output, you need to use "field templates", templates which represent the format you'd like to display.

Some common field templates are:

%s - string

%d - decimal number

%x - hexadecimal number

%o - octal number

%f - float number

You can also combine these field templates with numbers which indicate the number of digits to display - for example, %1.2f implies that Perl should only display two digits after the decimal point. If you'd like the formatted string to have a minimum length, you can tell Perl which character to use for padding by prefixing it with a single quote (').

 
 
>>> More Perl Programming Articles          >>> More By Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: