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.

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.nn";

@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.nn";

@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,5nn";

@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,5nn";

@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.

{mospagebreak title=Using Split() On a String}

In our previous examples, we used a single character as our delimiter. But we aren’t limited (pun intended) to that single character. Below are some examples using strings as a delimiter:


#!/usr/bin/perl

$Message = "Hidden Message: —-K—-i—-ll—-yo—-urse—-lfnn";

@DoIt = split(/—-/,$Message);

print @DoIt[0];

print @DoIt[1];

print @DoIt[2];

print @DoIt[3]. " ";

print @DoIt[4];

print @DoIt[5];

print @DoIt[6];

Here our delimiter is “—-”. The output of this nefarious code is:

  Hidden Message: Kill yourself

You can also use a variable as your delimiter, like so:


#!/usr/bin/perl

$Message = "Hidden Message: —-K—-i—-ll—-yo—-urse—-lfnn";

$Del = "—-";

@DoIt = split(/$Del/,$Message);

print @DoIt[0];

print @DoIt[1];

print @DoIt[2];

print @DoIt[3]. " ";

print @DoIt[4];

print @DoIt[5];

print @DoIt[6];

This gives us the same result as before.

If you want to get creative, you could also use a list. In this next example, we will create a string with several possible delimiters, and then use a list to switch back and forth between them:


#!/usr/bin/perl

$Data = "Here is some data: Apple,9~Beer,9~Cidernn";

@Del = (‘,’,’9′,’~',);

@Comma = split(/@Del[0]/,$Data);

@Nine = split(/@Del[1]/,$Data);

@Weird = split(/@Del[2]/,$Data);

print @Comma[0];

print @Comma[1];

print @Comma[2];

print @Comma[3];

print @Nine[0];

print @Nine[1];

print @Nine[2];

print @Nine[3];

print @Weird[0];

print @Weird[1];

print @Weird[2];

print @Weird[3];

This results in the following:

  Here is some data: Apple9~Beer9~Cider

  Here is some data: Apple,~Beer,~Cider

  Here is some data: Apple,9Beer,9Cider

{mospagebreak title=Limiting the Amount of Splits}

Sometimes you might not wish to split the entire string. Say you just want the first two delimited results, and not all of them. Here is how you would get those results:


#!/usr/bin/perl

$Some = "Hamburger–Fries–Tofu–Banana";

@Yum = split(/–/,$Some,3);

print @Yum[0] . "n";

print @Yum[1] . "n";

print @Yum[2] . "n";

In this example, the result is:

  Hamburger

  Fries

  Tofu–Banana

What happens here is that the function splits the first and second words and puts them into the first and second ([0] and [1]) positions in the @Yum array. It then stops seeking the delimiter and places whatever is left over into the third position of our array. That is why Tofu and Banana are still together, and indeed, even have their delimiter as well.

We can also use this method to assign values to a group of variables. Let’s say we have a string that lists several types of meat and we want each type of meat in its own variable. Here is how we would achieve this:


#!/usr/bin/perl

$Some = "Hamburger Pork Chicken Fish";

($Meat, $Pork, $Poultry, $Fish) =

split(/t/,$Some,4);

print $Meat . "n";

print $Pork . "n";

print $Poultry . "n";

print $Fish . "n";

The result is:

  Hamburger

  Pork

  Chicken

  Fish

You may have noticed something bizarre in the code where we usually place our delimiter. There is still a delimiter here; we are simply delimiting by the special character t, or tab.

You can also use split() on text files, but we will save that for a future tutorial.

{mospagebreak title=Assigning a List to Another List}

This is a pretty simple thing to do. Say you have a list of your grades for the semester. As you take your tests and whatnot, you want to create a new list showing your grades up to a certain point. Here is how you would do so:


#!/usr/bin/perl

@FirstMonth=(‘A ‘,’B ‘,’A ‘,’D ‘);

@SecondMonth=(‘A ‘,’B ‘,’C ‘,’A ‘,@FirstMonth);

print @SecondMonth;

This adds the values in @FirstMonth to the end of the @SecondMonth list, resulting in:

  A B C A A B A D

We could continue this process as the months go on:


#!/usr/bin/perl

@FirstMonth=(‘A ‘,’B ‘,’A ‘,’D ‘);

@SecondMonth=(‘A ‘,’B ‘,’C ‘,’A ‘,@FirstMonth);

@ThirdMonth=(‘F ‘,’F ‘,’A ‘,’A ‘,@SecondMonth);

@FourthMonth=(‘A ‘,’A ‘,’A ‘,’A ‘,@ThirdMonth);

print @FourthMonth;

You will note that I did not write @FourthMonth=(‘A’,'A’,'A’,'A’,@FirstMonth,@SecondMonth,@ThirdMonth). This is because it would have been redundant and added even more fields. Remember that the @SecondMonth already contains all of @FirstMonth’s value. And likewise, @ThirdMonth contains all of @FirstMonth and @SecondMonth’s values. And so forth.

The result of this code is:

  A A A A F F A A A B C A A B A D

And while we are at it, we can also assign variables to the mix as well. Here it is in code:


#!/usr/bin/perl

@FirstMonth=(‘A ‘,’B ‘,’A ‘,’D ‘);

@SecondMonth=(‘A ‘,’B ‘,’C ‘,’A ‘,@FirstMonth);

@ThirdMonth=(‘F ‘,’F ‘,’A ‘,’A ‘,@SecondMonth);

@FourthMonth=(‘A ‘,’A ‘,’A ‘,’A ‘,@ThirdMonth);

$ExtraCredit="A";

@Total=(@FourthMonth, $ExtraCredit);

print @Total;

The end result:

  A A A A F F A A A B C A A B A D A

Looks like you had some extra-curricular activity in their a few times with those F’s. Wink, wink.

Well, that’s all the time we have for this one. We still have a ways to go on Lists and Hashes, including Multi-Dimensional lists and the List::Util, but we’re making progress. Be sure to join me next time as we continue, and hopefully one day, finish this discussion.

Till then…

Google+ Comments

Google+ Comments