Free Perl Tutorials

Perl: Substr() function

This is a page dedicated to the substr() function and what it does in PERL.

What Substr() does: Substr() trims information from a string variable, and trim that information into a useful piece of information.

When to use the Substr() function: Use it when you are looking for just a small piece of information in a long variable.

Let's say for instance, we want a customer ID from a web site sub domain, here is how we would do it:

URL of interest: www.example.com/locator/location.asp?id=1234

We want to have Substr() look at the above line of code, and pull in just the id.

Here is what your PERL code would look like, and I will explain how it works below:

#! C:\PERL\bin\perl

# DEFINE A STRING TO REPLACE
$url = "http://www.example.com/locator/location.asp?id=1234";

# This will print a copy of our original string
print "Original String: $url";

# Saves a substring of $url, counting out 47 characters
#to skip before it saves the variable
$shorty = substr($url, 47);
print "The Customer Id: $shorty";


The Result is:
Original String: http://www.example.com/locator/location.asp?id=1234 The Customer Id: 1234

Now, lets see how the Substr() function is used and implemented in PERL:

#! C:\PERL\bin\perl

Is telling the script where to find the PERL directives to execute the code in windows - there is a separte code for those of you using linux

$url = "http://www.example.com/locator/location.asp?id=1234";

Is saving the "$url" variable to the value "http://www.example.com/locator/location.asp?id=1234"

print "Original String: $url";

This code is telling perl to print to your screen a bit of text and the "$url" variable which we just defined"

$shorty = substr($url, 47);
print "The Customer Id: $shorty";

This code assigns the shortened value to the variable "$shorty" and that value is just what we are looking for, the customer ID "1234". It then prints the information to your screen.

The above example is the simplest example of the substr function in PERL. Here are a few other parameters you can place on this function to make it more valuable.

How to set a specific length of the returned variable in the PERL substr() function:

$shorty = substr($url, 47,2);
print "The Customer Id: $shorty";

This code adds the length parameter. In other words, it looks at where to start the variable data collection (at character 47), and then tells how many characters after 47 to collect (2). The result of the above code is "12"

One other function parameter that is useful in the substr() function is the ability to Replace a value within a string. Here is an example of how we would do that

$shorty = substr($url, 11,7,"BS");
print "The Customer Id: $url";

This code locates the data at line 11 (where "example" starts), pulls 7 characters after that (example), and replaces it with "BS" Note: Make sure you are changing the variable in your print statement from $shorty to $url!

The returned value for $url is "http://www.bs.com/locator/locations.asp?id=1234"



I hope this tutorial has helped to enlighten you as to how you can use the substr() function. It can be complicated, though a very useful perl function!