Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 60
The Melonfire Community - Trog
Logo         Community
  Trog


Copyright notice:

This article is copyright Melonfire,
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 119
2018. All rights reserved.

All source code, brand names, trademarks and other content contained herein is proprietary to Melonfire,
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 121
2018. All rights reserved.

Source code within this article is provided with NO WARRANTY WHATSOEVER. It is meant for illustrative purposes only, and is NOT recommended for use in production environments.

Copyright infringement is a violation of law.

Printed from http://www.melonfire.com/community/columns/trog/article.php?id=190



String Processing With Perl
Perl comes with a powerful string manipulation API. Be afraid.


Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 150

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 152

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 167

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 168

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 170
Perl Of Wisdom

When Larry Wall first introduced Perl to an unsuspecting world, he had only one goal in mind: to create a language that simplified the task of extracting text and formatting it for display by simultaneously combining the best of sed, awk and C. The result of his efforts, the Practical Extraction and Reporting Language, was so powerful that developers all over the world started using it...and not just for text processing either!

Over the next few years, Perl underwent quite a few rewrites, gradually evolving into a full-featured programming language that could handle almost everything you threw at it. A modular system allowed developers to add to and enhance the language's features with their own custom code, thereby ensuring that the language was always at the forefront of new technologies, and guaranteeing a continuous stream of new converts. As a result, by 1995, Perl was a de facto component of almost every developer's toolkit.

Throughout all this, though, Perl stayed true to its roots. The language was originally designed for string processing and, today, still boasts one of the more powerful string handling APIs in the business. Perl's constructs and functions can deal with strings faster and more efficiently than those in competing languages, and its speed at text processing - string manipulation, comparison, sorting, extracting - make it a great choice for Web developers, who find both speed and power essential to their daily development activities.

Over the next few pages, this article will offer you a broad overview of Perl's string manipulation capabilities, serving as both a handy reference and a tool to help you write more efficient code. Regardless of whether you're new to Perl or if you've been working with the language for a while, you should find something interesting in here.

Let's get started!


Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 150

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 152

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 167

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 168

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 170


Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 150

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 152

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 167

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 168

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 170
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.


#!/usr/bin/perl

$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:


Look Ma, no hand


Now, how about chomp()?


#!/usr/bin/perl

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

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

# process the answer...
chomp($number);
$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.


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.


#!/usr/bin/perl

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

# returns 53
print length($str);



Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 150

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 152

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 167

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 168

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 170
Making New Friends

The split() function splits a string into smaller components on the basis of a user-specified pattern, and then returns these elements as an array.


#!/usr/bin/perl

$str = "I'm not as think as you stoned I am";

# split into individual words on whitespace delimiter and store in array
@words
@words = split (/ /, $str);


This function is particularly handy if you need to take a string containing a list of items (for example, a comma-delimited list) and separate each element of the list for further processing.

Here's an example:


#!/usr/bin/perl

$str = "Rachel,Monica,Phoebe,Joey,Chandler,Ross";

# split into individual words and store in array
@arr = split (/,/, $str);

# print each element of array
foreach $item (@arr)
{
        print("$item\n");
}


Here's the output:


Rachel
Monica
Phoebe
Joey
Chandler
Ross


Obviously, you can also do the reverse - the join() function creates a single string from all the elements of an array, glueing them together with a user-defined separator. Reversing the example above, we have:


#!/usr/bin/perl

@arr = ("Rachel", "Monica", "Phoebe", "Joey", "Chandler", "Ross");

# create string from array
$str = join (" and ", @arr);

# returns "Rachel and Monica and Phoebe and Joey and Chandler and Ross are friends"
print "$str are friends";



Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 150

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 152

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 167

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 168

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 170
Not My Type

The chr() and ord() functions come in handy when converting from ASCII codes to characters and vice-versa. For example,


#!/usr/bin/perl

# returns "A"
print chr(65);

# returns 97
print ord("a");


If you prefer numbers to letters, you can use the hex() and oct() functions to convert between decimals, hexadecimals and octals.


#!/usr/local/bin/perl

#returns 170
print hex(AA);

# returns 40
print oct(50);


And if you ever find the need to reverse a string, well, you can always reach for the reverse() function...


#!/usr/bin/perl

$str = "Wassup, dood?";

# reverse string
# $rts now contains ?dood ,pussaW
$rts = reverse($str);

# returns "Sorry, you seem to be talking backwards - what does ?dood ,pussaW mean?"
print "Sorry, you seem to be talking backwards - what does $rts mean?";



Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 150

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 152

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 167

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 168

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 170
Of Jumping Cows And Purple Pumpkins

Next up, the substr() function. As the name implies, this is the function that allows you to slice and dice strings into smaller strings. Here's what it looks like:


substr(string, start, length)


where "string" is a string or string variable, "start" is the position to begin slicing at, and "length" is the number of characters to return from "start".

Here's an example which demonstrates how this works:


#!/usr/bin/perl

$str = "The cow jumped over the moon, purple pumpkins all over the world rejoiced.";

# returns "purple pumpkin"
print substr($str, 30, 14);


You can use the case-sensitive index() function to locate the first occurrence of a character in a string,


#!/usr/bin/perl

$str = "Robin Hood and his band of merry men";

# returns 29
print index($str, "r");


and the rindex() function to locate its last occurrence.


#!/usr/bin/perl

$str = "Robin Hood and his band of merry men";

# returns 33
print  rindex($str, "m");


You can also tell these functions to skip a certain number of characters before beginning the search - consider the following example, which demonstrates by beginning the search after skipping the first five characters in the string.


#!/usr/bin/perl

$str = "Robin Hood and his band of merry men";

# skips 5 "o"s
# returns 7 for the first "o" in "Hood"
print index($str, "o", 5);



Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 150

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 152

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 167

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 168

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 170


Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 150

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 152

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 167

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 168

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 170
Desperately Seeking Susan

You can also search for specific patterns in your strings with regular expressions, something that Perl supports better than most other languages.

In Perl, all interaction with regular expressions takes place via an equality operator, represented by =~


$flag =~ m/susan/


$flag returns true if $flag contains "susan" using the "m" operator.

You can also perform string substitution with regular expressions with the "s" operator, as in the following exanple.


$flag =~ s/susan/JANE/


This replaces "susan" in the variable $flag with "JANE" using the "s" operator.

Here is a simple example that validates an email address:


#!/usr/bin/perl

# get input
print "So what's your email address, anyway?\n";
$email = <STDIN>;
chomp($email);

# match and display result
if($email =~
/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/)
{
   print("Ummmmm....that sounds good!\n");
} else
{
   print("Hey - who do you think you're kidding?\n");
}


Obviously, this is simply an illustrative example - if you're planning to use it on your Web site, you need to refine it a bit. You have been warned!

If you want to find out the number of times a particular pattern has been repeated in a string, Perl offers the very cool "tr" operator.


#!/usr/bin/perl

# get input
print "Gimme a string: ";
$string = <STDIN>;
chomp($string);

# put string into default variable for tr
$_ = $string;

# check string for spaces and print result
$blanks += tr/ / /;
print ("There are $blanks blank characters in \"$string\".");


Here's an example session:


Gimme a string: This is a test.
There are 3 blank characters in "This is a test.".


You can have Perl return the position of the last match in a string with the pos() function,


#!/usr/bin/perl

$string = "The name's Bond, James Bond";

# search for the character d
$string =~ /d/g;

# returns 15
print pos($string);


and automatically quote special characters with backslashes with the quotemeta() function.


#!/usr/bin/perl

$string = "#@!#@!#@!";

$string = quotemeta($string);

# returns \#\@\!\#\@\!\#\@\!
print $string;


Sadly, that's about all we have time for. In case you want more, consider visiting the following links:

The Perl string API, at http://www.perldoc.com/perl5.6/pod/perlfunc.html

The Perl 101 series, at http://www.melonfire.com/community/columns/trog/article.php?id=5

A discussion of regular expressions, at http://www.melonfire.com/community/columns/trog/article.php?id=2

Until next time...be good.

Note: Examples are illustrative only, and are not meant for a production environment. Melonfire provides no warranties or support for the source code described in this article. YMMV!


Copyright notice:

This article is copyright Melonfire,
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 193
2018. All rights reserved.

All source code, brand names, trademarks and other content contained herein and proprietary to Melonfire,
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 195
2018. All rights reserved.

Source code within this article is provided with NO WARRANTY WHATSOEVER. It is meant for illustrative purposes only, and is NOT recommended for use in production environments.

Copyright infringement is a violation of law.

Printed from http://www.melonfire.com/community/columns/trog/article.php?id=190



Copyright © 1998-
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 211
2018 Melonfire. All rights reserved
Terms and Conditions | Feedback