Perl
  Home arrow Perl arrow Page 3 - Perl: Concatenating Text and More
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: Concatenating Text and More
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-03-10

    Table of Contents:
  • Perl: Concatenating Text and More
  • Making Copies
  • Chomping It Up Pac-Man Style
  • Transformers...More than Meets the Eye

  • 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

    Virtual Tradeshows by Ziff Davis Enterprise – A Unique Opportunity to Interact with IT Experts, Access Information, and Gain Insight on Today’s Trends in Technology Learn more

    Perl: Concatenating Text and More - Chomping It Up Pac-Man Style
    (Page 3 of 4 )

    If you want to chop off the end part of a string, you can do so using either the chop or chomp function. The chop function removes the last character of a string and returns what it chopped off, like so:


    #!/usr/bin/perl

     

    $scary="Look out it's Lorena Bobbitt";

    $a=chop($scary);

    print $scary;

    print "\nOh God...I chopped off a $a! Now she's gonna be pissed!";

    This will print out:

      Look out it's Lorena Bobbit

      Oh God...I chopped off a t! Now she's gonna be pissed!

    In the above example we assigned a value to the variable $scary, then used the chop function to chop off the last character and assign that removed character to the variable $a. We then printed $scary and a sentence with the chopped off character in it.

    Chomp works in a similar fashion, and is said to be safer than using chop, as chomp removes two line-ending characters. Consider this code, which is the same as the above code, only I have added a newline escape character to it, to insert a space between text:


    #!/usr/bin/perl

     

    $scary="Look out it's Lorena Bobbitt\n";

    $a=chop($scary);

    print $scary;

    print "\nOh God...I chopped off a $a! Now she's gonna be pissed!";

    This prints out:

      Look out it's Lorena Bobbitt

      Oh God...I chopped off a

      ! Now she's gonna be pissed!

    As you can see, in this instance chop took our newline character instead of the t as we had intended. And instead of returning a t as I had wanted, it returned a newline, messing up the flow of my text. This is why it is safer to use chomp. Here it is in action:


    #!/usr/bin/perl

     

    $scary="Look out it's Lorraina Bobbittn";

    $a=chomp($scary);

    print $scary;

    print "\nOh God...I chopped off a $a! Now she's gonna be pissed!";

    Tada! Note that if you have Perl version 4, chomp doesn't work.

    More Perl Articles
    More By James Payne


       · Hi, thanks for droppingby to read my Perl article. Inside we discuss how to...
     

       

    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 3 hosted by Hostway