Perl
  Home arrow Perl arrow Page 6 - Perl 101 (part 6) - The Perl Toolbox
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 101 (part 6) - The Perl Toolbox
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2000-08-30

    Table of Contents:
  • Perl 101 (part 6) - The Perl Toolbox
  • Expressing Yourself
  • Engry Young Men
  • Aardvark, Anyone?
  • Needles In Haystacks
  • Slice And Dice
  • Going Backwards
  • Math Class

  • 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

    Perl 101 (part 6) - The Perl Toolbox - Slice And Dice
    (Page 6 of 8 )

    Next up, the substr() function. As the name implies, this is the function that allows you to slice and dice strings into smaller strings. Here's what it looks like:
    
    substr(string, start, length)
    

    where "string" is a string or a scalar variable containing a string, "start" is the position to begin slicing at, and "length" is the number of characters to return from "start".

    Here's a Perl script that demonstrates the substr() operator:
    
    #!/usr/bin/perl
    # get a string
    print "Gimme a line!\n";
    $line = ;
    chomp ($line);
    # get a chunk size
    print "How many characters per slice?\n";
    $num_slices = ;
    chomp ($num_slices);
    $length = length($line);
    $count = 0;
    print "Slicing...\n";
    # slice the string into sections
    while (($num_slices*$count) < $length)
    {
    $temp = substr($line, ($num_slices*$count), $num_slices);
    $count++;
    print $temp . "\n";
    }
    

    Here, after getting a string and a block size, we've used a "while" loop and a counter to keep slicing off pieces of the string and displaying them on separate lines.

    And here's what it looks like:
    
    Gimme a line!
    The cow jumped over the moon, giggling madly as a purple pumpkin with fat
    ears exploded into confetti
    How many characters per slice?
    11
    Slicing...
    The cow jum
    ped over th
    e moon, gig
    gling madly
    as a purpl
    e pumpkin w
    ith fat ear
    s exploded
    into confet
    ti
    

    You've already used the print() function extensively to sent output to the console. 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 programmers came up with the printf() function, which allows you to define the format in which data is printed to the console.

    Consider a simple example - printing decimals:
    
    #!/usr/bin/perl
    print (5/3);
    

    And here's the output:
    
    1.66666666666667
    

    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 printf() function:
    
    #!/usr/bin/perl
    printf "%1.2f", (5/3);
    

    which returns
    
    1.67
    

    A quick word of explanation here: the Perl printf() 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
    %c character
    %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.

    Here are a few more examples of printf() in action:
    
    printf("%05d", 3); # returns 00003
    printf("$%2.2f", 25.99); # returns $25.99
    printf("%2d%", 56); # returns 56%
    

    And here's a calculator which uses the printf() function to display numbers in various numerical bases like hexadecimal and octal.
    
    #!/usr/bin/perl
    print "Enter a number: ";
    chomp($number = );
    printf("In decimal format: %d\n",$number);
    printf("In hexadecimal format: %x\n",$number);
    printf("In octal format: %o\n",$number);
    

    Perl also comes with a sprintf() function, which is used to send the formatted output to a variable instead of standard output.

    This article copyright Melonfire 2000. All rights reserved.

    More Perl Articles
    More By Vikram Vaswani and 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 5 hosted by Hostway