Perl
  Home arrow Perl arrow Page 2 - 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 - Jumping Jacks
    (Page 2 of 8 )

    We'll begin right at the top, with some very basic definitions and concepts.

    In Perl, the term "string" refers to a sequence of characters. The following are all valid examples of strings:


    "I'm back!" "by golly miss molly" "a long time ago in a galaxy far, far away"
    String values can be assigned to a variable using the standard assignment operator.

    $identity = "Jedi";
    String values may be enclosed in either double quotes ("") or single quotes('') - the following variable assignments are equivalent"

    $character = "Luke"; $character = 'Luke';
    String values enclosed in double quotes are automatically parsed for variable names; if variable names are found, they are automatically replaced with the appropriate variable value.

    #!/usr/bin/perl $character = "Chewbacca"; $race = "Wookie"; # this would contain the string "Chewbacca is a Wookie" $sentence = "$character is a $race";
    Perl also allows you to create strings which span multiple lines. The original formatting of the string, including newlines and whitespace, is retained when such a string is printed.

    # multi-line block $html_output = <<EOF; <html> <head></head> <body> <ul> <li>Human <li>Wookie <li>Ewok </ul> </body> </html> EOF
    The << symbol indicates to Perl that what comes next is a multi-line block of text, and should be printed as is right up to the marker "EOF". This comes in very handy when you need to output a chunk of HTML code, or any other multi-line string.

    Strings can be concatenated with the string concatenation operator, represented by a period(.)

    #!/usr/bin/perl # set up some string variables $a = "the cow "; $b = "jumped over "; $c = "the moon "; # combine them using the concatenation operator # this returns "the cow jumped over the moon" $statement = $a . $b . $c; # and this returns "the moon jumped over the cow" $statement = $c . $b . $a;
    Note that if your string contains quotes, carriage returns or backslashes, it's necessary to escape these special characters with a backslash.

    # will cause an error due to mismatched quotes $film = 'America's Sweethearts'; # will be fine $film = 'America\'s Sweethearts';
    The print() function is used to output a string or string variable.

    #!/usr/bin/perl # string print "Last Tango In Paris"; # string variable $film = "Last Tango In Paris"; print $film;
    But if you thought that all you can do is concatenate and print strings, think again - you can also repeat strings with the repetition operator, represented by the character x.

    #!/usr/bin/perl # set a string variable $insult = "Loser!\n"; # repeat it print($insult x 7);
    Here's the output:

    Loser! Loser! Loser! Loser! Loser! Loser! Loser!
    Nasty, huh?

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