Home arrow Perl Programming arrow Page 3 - Perl 101 (part 6) - The Perl Toolbox

Engry Young Men - Perl

This time, Perl 101 visits some of Perl's more useful in-builtfunctions, and teaches you the basics of pattern matching and substitution.Also included is a list of common string and math functions, together withexamples of how to use them.

TABLE OF CONTENTS:
  1. Perl 101 (part 6) - The Perl Toolbox
  2. Expressing Yourself
  3. Engry Young Men
  4. Aardvark, Anyone?
  5. Needles In Haystacks
  6. Slice And Dice
  7. Going Backwards
  8. Math Class
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 6
August 30, 2000

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
In addition to simple matching, Perl also allows you to perform substitutions.

Take a look at our next example, which prompts you to enter a line of text, and then replaces all occurrences of the letter "a" with the letter "e".


#!/usr/bin/perl
# get a line of input
print "Gimme a line!\n";
$line = ;
chomp ($line);
# substitute with the substitution operator
$line =~ s/a/e/;
# and print
print $line;

In this case, we've used Perl's substitution operator - it looks like this:
s/search-pattern/replacement-pattern/

In the example above, the line
$line =~ s/a/e/;

simply means "substitute a with e in the scalar variable $line".

And here's the output:



Gimme a line!
angry young man
engry young man

Ummm...didn't work quite as advertised, did it? All it did was replace the first occurrence of the letter "a". How about adding the "g" operator, which does a global search-and-replace?


#!/usr/bin/perl
# get a line of input
print "Gimme a line!\n";
$line = ;
chomp ($line);
# substitute with the substitution operator
$line =~ s/a/e/g;
# and print
print $line;

And this time,

angry young man

is replaced with

engry young men

Much better! But what if your sentence contains an upper-case "a", also known as "A". Well, that's why Perl also has the case-insensitivity operator "i", which takes care of that last niggling problem:


#!/usr/bin/perl
# get a line of input
print "Gimme a line!\n";
$line = ;
chomp ($line);
# substitute with the substitution operator
$line =~ s/a/e/gi;
# and print
print $line;

And the output:

Gimme a line!
Angry young man
Engry young men

Of course, we're just scratching the tip of the regex iceberg here. Things get even more interesting when you start using patterns and metacharacters instead of actual words...

This article copyright Melonfire 2000. All rights reserved.

 
 
>>> More Perl Programming Articles          >>> More By Vikram Vaswani and 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: