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

    Dell PowerEdge Servers

    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

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