Perl
  Home arrow Perl arrow Page 3 - Perl 101 (part 6) - The Perl Toolbox
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

Perl 101 (part 6) - The Perl Toolbox
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2000-08-30

    Table of Contents:
  • Perl 101 (part 6) - The Perl Toolbox
  • Expressing Yourself
  • Engry Young Men
  • Aardvark, Anyone?
  • Needles In Haystacks
  • Slice And Dice
  • Going Backwards
  • Math Class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Perl 101 (part 6) - The Perl Toolbox - Engry Young Men
    (Page 3 of 8 )

    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 Articles
    More By Vikram Vaswani and Harish Kamath, (c) Melonfire


     

       

    PERL ARTICLES

    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI
    - Perl: Concatenating Text and More
    - Perl Text: Quoting Without Quote Marks




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway