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.