Perl
  Home arrow Perl arrow Page 5 - 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 - Needles In Haystacks
    ( Page 5 of 8 )

    Perl comes with a wide variety of functions that come in handy when manipulating strings. The first one on our list is the length() function, which returns the length of a specified string.

    Here's an example of how the length() function can be used to restrict the length of a login name to between six and ten characters:


    #!/usr/bin/perl
    
    do
    {
    
    # ask for a name
           print ("Please enter a username:");
    $username = ;
    chomp($username);
    
    # and repeat until the username is between 6 and 10 characters long
    } while ((length($username) < 6) ||  (length($username) > 10));
    
    
    print "A new star is born...and its name is $username!\n";
    
    

    In this case, each time a username is entered, we use the length() function to count the number of characters in it. If this number is less than 6, or greater than 10, the loop is repeated until a username of the correct length is entered.

    Here's what it looks like:
    
    Please enter a username:me
    Please enter a username:galapaloozy
    Please enter a username:godzilla
    A new star is born...and its name is godzilla!
    

    The next string function that we're going to unravel is the index() function. This function is typically used to find out if a particular pattern exists within a string. Here's what it looks like:
    
    $var = index(string, pattern)
    

    where "string" is the string to be searched for "pattern". If the pattern is found within the string, the position of the first character of the matched pattern will be assigned to the variable $var; if not, $var will be assigned the value -1.

    Here's a quick example:
    #!/usr/bin/perl
    
    # how to find a needle in a haystack
    # Perl-style
    
    print "THE HAYSTACK\n";
    print "------------\n";
    
    # set up the string
    $haystack = "211643831 923465971315874643 13729247620352625
    9235923595232305232095 8284529 2347392901847
    32393482562502925352395327202358";
    print $haystack . "\n";
    
    # ask for a search term
    print "Gimme a needle: ";
    $needle = ;
    chomp ($needle);
    
    # use index() to look for the string
    $location = index($haystack, $needle);
    
    # print appropriate message
    if ($location >= 0)
    {
    print "Needle located $location characters deep in the haystack\n";
    }
    else
    {
    print "Sorry, this haystack contains no needle\n";
    }
    
    

    And here's the output:
    
    THE HAYSTACK
    ------------
    211643831 923465971315874643 13729247620352625 9235923595232305232095
    8284529
    2347392901847 32393482562502925352395327202358
    Gimme a needle: 1267
    Sorry, this haystack contains no needle
    
    THE HAYSTACK
    ------------
    211643831 923465971315874643 13729247620352625 9235923595232305232095
    8284529
    2347392901847 32393482562502925352395327202358
    Gimme a needle: 6250
    Needle located 101 characters deep in the haystack
    
    

    In this case, we've set up a string containing a set of random numbers. The user is then asked to enter a number of his own choice, and the index() function is used to scan the string for the number. Depending on the result, an appropriate message is printed.

    Similar to the index() function is the rindex() function, which also searches for a pattern within the specified string, but starts from the end.



    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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek