Logo         Community
The Company
Your Account
Contact Us
String Processing With Perl
Perl comes with a powerful string manipulation API. Be afraid.

| Choppy Waters |

With the basics out of the way, let's now turn to some of the other string functions available in Perl. Other than print(), the two functions you're likely to encounter most often are chop() and chomp().

There's a very subtle difference between these two functions. Take a look at this next example, which demonstrates chop() in action.


$statement = "Look Ma, no hands";

print chop($statement);

The chop() function removes the last character from a string and returns the mutilated value - as the output of the above program demonstrates:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
Look Ma, no hand

Now, how about chomp()?


# ask a question...
print "Gimme a number! ";

# get an answer...
$number = <STDIN>;

# process the answer...
$square = $number * $number;

# display the result
print "The square of $number is $square\n";

In this script, prior to using the chomp() function, the variable $number contains the data entered by the user at the prompt, together with a newline (\n) character caused by pressing the Enter key. Before the number can be processed, it is important to remove the newline character, as leaving it in could adversely affect the rest of the program. Hence, chomp().

The chomp() function's sole purpose is to remove the newline character from the end of a variable, if it exists. Once that's taken care of, the number is multiplied by itself, and the result is displayed.

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
Gimme a number! 5
The square of 5 is 25

The length() function returns the length of a particular string, and can come in handy for operations which involve processing every character in a string.


$str = "The wild blue fox jumped over the ripe yellow pumpkin";

# returns 53
print length($str);

How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!

previous page more like this  print this article  next page
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
In cut!...
American Chai
The Core
Find out how you can use this article on your own Web site!

Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback