Perl
  Home arrow Perl arrow Perl: Concatenating Text and More
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Perl: Concatenating Text and More
    ( Page 1 of 4 )

    This marks the finale of our coverage of text in Perl (until we get into some more of the advanced Perl features at any rate). We left off with the here document and how to use it to display text exactly as we type it in, using a Mark Twain poem that he had written for his daughter's tombstone (which interestingly enough was a rewrite of another poet's poem). We also learned a little bit about ASCII and the values as they pertain to text, showcasing the 93 visible characters.

    In this tutorial we are going to pick up on our discussion of ways to manipulate strings, starting with concatenation, or combining, and working our way through to converting strings. It's a lot to cover in a short time, so let's get to work.

    Concatenation...The Function You Can Barely Spell

    A lot of the string functions resemble mathematical equations. For instance, our first batter up is pretty similar to a simple addition. When you concatenate, all you are doing is adding two strings together. Here is an example: 


    #!/usr/bin/perl

     

    print "My name is " . "whicka whicka " . "Mr. Sippi.";

    This results in the following being displayed to your screen:

      My name is whicka whicka Mr. Sippi.

    You can also assign a variable to another variable through concatenation. In this next example, we are going to display the slightly repetitive lyrics of the Spaniels song, "Goodnight Sweetheart, Goodnight":


    #!/usr/bin/perl

     

    $a='Goodnight sweetheart';

    $b="Well, it's time to go";

    $c="\n";

    $d='I hate to leave you, but I really must say';

    $e= $a . ', goodnight';

    print $a;

    print $c . $b;

    print $c . $a;

    print $c . $b;

    print $c . $d;

    print $c . $e;

    As you can see, the concatenation occurs with the variable $e, in which we add the string ', goodnight' to the variable $a. Afterward the variable $e holds the value: "Goodnight sweetheart, goodnight". This is the result of our program:

      Goodnight sweetheart

      Well, it's time to go

      Goodnight sweetheart

      Well, it's time to go

      I hate to leave you, but I really must say

      Goodnight sweetheart, goodnight

    Note that when you have double quoted strings, you don't always need to concatenate. Observe this sample:


    #!/usr/bin/perl

     

    $a='Big ';

    $b='Macs';

    print 'I like to eat ' . $a . $b;

    This prints out:

      I like to eat Big Macs

    If we had used double quotes, we could have accomplished the same thing like this:


    #!/usr/bin/perl

     

    $a='Big ';

    $b='Macs';

    print "I like to eat $a $b";

    Printing this:

      I like to eat Big Macs

    without having to use the concatenating operator (.).

    Remember that single quotes do not interpret, so had you tried that method with single quotes, like this:


    #!/usr/bin/perl

     

    $a='Big ';

    $b='Macs';

    print 'I like to eat $a $b';

    Your result would have been:

      I like to eat $a $b

    Which don't taste anywhere near as good.

    And lastly, if you want to concatenate a variable to another variable, you can use the concatenate assignment operator (.=), as in the following example:


    #!/usr/bin/perl

     

    $a='Big ';

    $b='Macs';

    $a .= $b;

    print "I like to eat $a";

    Which is really just shorthand for:


    #!/usr/bin/perl

     

    $a='Big ';

    $b='Macs';

    $a = $a . $b;

    print "I like to eat $a";

    It might not seem like a big difference, but when you begin adding a lot of variables to one another and get into more complicated coding, it really makes a difference.



     
     
    >>> More Perl Articles          >>> More By James Payne
     

       

    PERL ARTICLES

    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl
    - Perl: More on Lists and Hashes
    - Perl: Dimensional Lists





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT