Home arrow Perl Programming arrow Page 5 - Perl 101 (part 6) - The Perl Toolbox

Needles In Haystacks - Perl

This time, Perl 101 visits some of Perl's more useful in-builtfunctions, and teaches you the basics of pattern matching and substitution.Also included is a list of common string and math functions, together withexamples of how to use them.

TABLE OF CONTENTS:
  1. Perl 101 (part 6) - The Perl Toolbox
  2. Expressing Yourself
  3. Engry Young Men
  4. Aardvark, Anyone?
  5. Needles In Haystacks
  6. Slice And Dice
  7. Going Backwards
  8. Math Class
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 6
August 30, 2000

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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 Programming Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: