Monday, November 21, 2011

PERL SPLIT OPERATOR

PERL SPLIT OPERATOR


 

REVISED: Sunday, March 3, 2013




You will learn how to use the Perl split operator.

I.  PERL SPLIT OPERATOR

The Perl split operator is commonly used when parsing data from a file or from another program.  The Perl split operator allows you to break up a string into an array or a list, by using a specific pattern.

The split operator is used to split a string into smaller sections.  You can split a string on a single character, a group of characters or a regular expression, a pattern.  You can also specify how many pieces to split the string into.

The Perl split operator syntax is as follows:

LIST = split(/PATTERN/, EXPR, LIMIT);

LIST is a list, array or hash that is returned by the split operator.  If LIST is omitted, the Perl split operator will be called in a scalar context and the EXPR will be split in the @_ array.   If LIST is omitted the operator will return the number of fields in which the EXPR will be split.  If your call to a split operator is inside the body of a subroutine it alters the value of @_.

PATTERN is normally a regular expression; however,  PATTERN can be a single character or a string.  ILIMIT is not specified the EXPR is split on every occurrence of the PATTERN.  If the PATTERN parameter is omitted, the Perl split operator will split on whitespaces, skipping any leading whitespaces.

EXPR is the string expression that will be split into an array or a list.  If EXPR is omitted, the content of the special scalar variable $_ will be split.

LIMIT is the maximum number of fields the EXPR will be split into.
Only the word split is compulsory.  You can omit the parenthesis, the LIST that will be returned and any of the third arguments presented above.

If you use one of the following metacharacter inside the pattern you need to escape the metacharacter by using the \ backslash character to indicate that the metacharacter is to be regarded as something to match and not some fancy character:

^     $     ( )     \     |     @     [     {     ?     .     +     *

For example, if you want to split your string using the pattern /&&/ you need to write it as /\&\&/.

The Perl join operator is the opposite of the Perl split operator.  There are a lot of situations where they are used together.

The Perl join operator concatenates; i.e., joins the elements of a list into a single string.

The Perl split operator breaks up a single string into a list of strings and returns that list.

II.  PERL SPLIT OPERATOR SOURCE CODE

Copy paste the following into your text editor.

#Provides undefined value warnings.
use warnings;

#Helps you catch typos.
#Forces you to use my() function to declare all variables.
use strict;

#Specify the file.
my($file) = "C:/Strawberry/splitOperator.pl";
#Open the file and read data.
#Die with grace if it fails.
open (FILE, "<$file") or die "Can't open $file: $!\n";

#initialize array

my(@fields) = split(/-/, "808-253-4321");
print "The telephone number is: @fields\n\n";

#Close the file.
close FILE;

From your text editor, "File Save As" splitOperator.pl using the path to the folder of your Perl download.

From the Perl prompt type the following:

C:\WINDOWS\system32>C:\Strawberry\splitOperator.pl

Press Enter and the following will display on your screen:

C:\WINDOWS\system32>C:\Strawberry\splitOperator.pl
The telephone number is: 808 253 4321

C:\WINDOWS\system32>

You have learned how to use the Perl split operator.

Elcric Otto Circle


















-->   -->   -->







How to Link to My Home Page

It will appear on your website as:
"Link to ELCRIC OTTO CIRCLE's Home Page"






No comments:

Post a Comment