Perl
  Home arrow Perl arrow Page 3 - String Processing with Perl
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

String Processing with Perl
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 49
    2003-04-16

    Table of Contents:
  • String Processing with Perl
  • Jumping Jacks
  • Choppy Waters
  • Making New Friends
  • Not My Type
  • Of Jumping Cows And Purple Pumpkins
  • On The Case
  • Desperately Seeking Susan

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    String Processing with Perl - Choppy Waters
    (Page 3 of 8 )

    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 = ; # 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);

    More Perl Articles
    More By 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

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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