eWebProgrammer eweb  

Perl Variables  «Prev  Next»
Lesson 12Perl Split function
ObjectiveLesson describes application of the Perl Split Function

Perl Split Function

Write two programs using join and split, one of which writes an array to a file and the other which reads the array from the file.
The split function splits up a delimited string into a list (or an array). This is the syntax of the split function:
split /regexp/, SCALAR, limit
For example, the colon-delimited string from the previous lesson:
@array = ("Foo", "Bar", "Baz", "Boz");
print join(':', @array), "\n";

can be split like this:
$string = 'Foo:Bar:Baz:Boz';
@array = split(/:/, $string);

Finally, if included, the limit parameter can be used to limit the number of elements that will be created in the resulting list. For instance, this:
@array = split(/:/, $string, 3);

will result in three elements:
"Foo", "Bar", and "Baz:Bob".
In order to experiment with join and split, you will probably want to know how to read and write to files. Again, this is covered in greater detail later in the course, but we will cover the fundamentals of
simple file I/O here.
Exercise Completion
You will need the information presented in the file I/O sidebar to complete this lesson's exercise.
Perl Split Function - Exercise
Now that you have all the necessary data about join and split, try your hand at writing a pair of programs using them.
Perl Split Function - Exercise