The split and join Functions

The split function is one of the most useful functions in Perl. It splits up a string and turns it into an array. This makes it extremely useful when it comes to parsing things such as Excel export files, UNIX or Linux configuration files, or other similar text files.

For example, suppose that you have an inventory data file where the colon (:) separates each record. A typical entry might look like the following:

#10 screws:1000:500:Universal Screw Company

(This record has a listing for #10 screws. Currently 1000 are in stock. When the number reaches 500, you’ll reorder more from the Universal Screw Company.)

Now turn this record into an array. You do this by using the split function:

 @array = split /:/, $record; ...

Get Perl for C Programmers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.