Perl
  Home arrow Perl arrow Page 6 - Perl 101 (part 6) - The Perl Toolbox
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? 
Google.com  
PERL

Perl 101 (part 6) - The Perl Toolbox
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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


    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

    - More Perl Bits
    - Perl, Bit by Bit
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek