Home arrow Perl Programming arrow Perl Lists: The Split() Function

Perl Lists: The Split() Function

In this fourth part of our series on Lists, we will start off with the split() function and hopefully end by covering hashes. In our last article, we covered the splice() function, which we used to add, remove, and replace elements in a list. We then used it to create variables and arrays. We also worked with some operators to repeat a list and to create sequential lists.

TABLE OF CONTENTS:
  1. Perl Lists: The Split() Function
  2. Using Split() On a String
  3. Limiting the Amount of Splits
  4. Assigning a List to Another List
By: James Payne
Rating: starstarstarstarstar / 6
April 07, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Split()ting Hairs

The split() function has many uses in Perl. Its main purpose is to take a string and break it apart, returning a list of strings. In our first example, we will take a sentence and break each word into a list. Here it is in code:


#!/usr/bin/perl

$Trees = "Why aren't there any fat trees? All they do is eat all day

and sit around.\n\n";

@Pieces = split(/ /, $Trees);

print $Trees;

print @Pieces;

This code takes each word in the $Trees string and puts each one as an element in the @Pieces list. The split() function above has the following as its argument: (/ /, $Trees). The first part, / / is two forward slashes with a space between them. Whatever you place in between the forward slashes is used as a delimiter. So in this instance, split() looks at the $Trees variable, finds the first word, sees a space, then adds that word to the @Pieces first element. Next it sees the next word, finds a space after it, and adds that word as the second element, and so forth. The print out of the above text is:

  Why aren't there any fat trees? All they do is eat all day and sit around.

  Whyaren'tthereanyfattrees?Alltheydoiseastalldayandsitaround.

To better understand what is going on, let's modify the code a little bit:


#!/usr/bin/perl

$Trees = "Why aren't there any fat trees. All they do is eat all day

and sit around.\n\n";

@Pieces = split(/ /, $Trees);

print $Trees;

print @Pieces[0] . " ";

print @Pieces[1] . " ";

print @Pieces[2] . " ";

print @Pieces[3] . " ";

print @Pieces[4] . " ";

In the above example, the code is modified a bit so that we print out some of the individual elements in the @Pieces array. You will note that I appended a space after each element, just to make it more legible.

The end result is:

  Why aren't there any fat trees? All they do is eat all day and sit around.

  Why aren't there any fat

We can use anything as our delimiter when we use the split() function. In this next example, we use a comma, though this should be avoided as it can lead to mistakes if you have commas in your strings that you do not wish to have parsed and you forget about them (we'll discuss how to deal with this issue later):


#!/usr/bin/perl

$Numlist = "Here are some numbers: 1,2,3,4,5\n\n";

@Numbers = split(/,/,$Numlist);

print @Numbers[0] . " ";

print @Numbers[1] . " ";

print @Numbers[2] . " ";

print @Numbers[3] . " ";

This program results in:

  Here are some numbers: 1 2 3 4

In the above example, what do you think the value of @Number[0] is? If you think it is 1, think again. Try this code:


#!/usr/bin/perl

$Numlist = "Here are some numbers: 1,2,3,4,5\n\n";

@Numbers = split(/,/,$Numlist);

print @Numbers[0];

The result is:

Here are some numbers: 1

This is because the program searches for the delimiter and then takes the value. So everything preceding the delimiter is taken, hence our result.



 
 
>>> More Perl Programming Articles          >>> More By James Payne
 

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: