Free Perl Tutorials

Showing posts with label PERL. Show all posts
Showing posts with label PERL. Show all posts

Split() in PERL

This tutorial teaches you how to use a very handy function in PERL called the split function. PERL allows you to split up a string into individual values if you would so choose.

Here is the format for using the split function in PERL. Formatting is very important to ensure the function works properly. (Feel free to copy and paste this to a notepad, save the file as a .pl file, and then run it from your command prompt)

#First we define a string
$mystring = "This is my wonderful string";

#Next we incorporate the split function to divide the string at each space
#We have five words, so we'll create 5 new variables
($v1,$v2,$v3,$v4,$v5) = split(/ /,$mystring);

#Now we print our code to check our results (the "/n" creates a new line)
print "$v1\n"
print "$v2\n"
print "$v3\n"
print "$v4\n"
print "$v5\n"




Here's what's happening:
In our first line, we have set a string variable.

In the second line, we create 5 new variables ($v1-$v5) and set these equal to each word split from our string. We do this between the "//" characters. Since we want the split function to split the data at each space, we place a space between the "//" characters.

I found this very useful video of how to do it on youtube:

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!