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

String Processing with Perl
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 62
    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:
      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


    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

    - 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 1 hosted by Hostway
    Stay green...Green IT