Perl
  Home arrow Perl arrow 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

    A high performance database engine using optimized data access for all development environments including Delphi, Visual Studio .NET, Visual Basic, Visual FoxPro. and more. Learn More

    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


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

       

    PERL ARTICLES

    - 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
    - Perl: Releasing Your Inner Textuality

     
    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