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

    PCmover - $15 Off with Coupon Code CJPH7Q

    String Processing with Perl - On The Case
    (Page 7 of 8 )

    The next few string functions come in very handy when adjusting the case of a text string from lower- to upper-case, or vice-versa:

    lc() - convert string to lower case

    uc() - convert string to upper case

    ucfirst() - convert the first character of string to upper case

    lcfirst() - convert the first character of a string to lower case

    Here's an example:


    #!/usr/bin/perl $str = "Something's rotten in the state of Denmark"; # returns "something's rotten in the state of denmark" print lc($str); # returns "SOMETHING'S ROTTEN IN THE STATE OF DENMARK" print uc($str); # returns "something's rotten in the state of Denmark" print lcfirst($str); # re-initialize for next bit of code $str = "something's rotten in the state of Denmark"; # returns "Something's rotten in the state of Denmark" print ucfirst($str);
    You've already used the print() function extensively to display output. However, the print() function doesn't allow you to format output in any significant manner - for example, you can't write 1000 as 1,000 or 1 as 00001. And so clever Perl developers came up with the sprintf() function, which allows you to define the format in which data is printed.

    Consider the following example:

    #!/usr/bin/perl # returns 1.6666666666667 print(5/3);
    As you might imagine, that's not very friendly. Ideally, you'd like to display just the "significant digits" of the result. And so, you'd use the sprintf() function:

    #!/usr/bin/perl # returns 1.67 print sprintf("%1.2f", (5/3));
    A quick word of explanation here: the Perl's sprintf() function is very similar to the printf() function that C programmers are used to. In order to format the output, you need to use "field templates", templates which represent the format you'd like to display.

    Some common field templates are:

    %s - string

    %d - decimal number

    %x - hexadecimal number

    %o - octal number

    %f - float number

    You can also combine these field templates with numbers which indicate the number of digits to display - for example, %1.2f implies that Perl should only display two digits after the decimal point. If you'd like the formatted string to have a minimum length, you can tell Perl which character to use for padding by prefixing it with a single quote (').

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