Converting Between Strings and Arrays


Perl makes the process of splitting a string into parts (or tokens) and storing them in an array simple. It is just as simple to join the elements in an array together into a string.

Splitting Apart a String

split /PATTERN/,EXPR

The split function takes a regular expression (a text patern to look for) and a string and returns an array of strings. Regular expressions are described later. The PATTERN between the two forward slashes is the text pattern that separates the text. Here is an example: 
right click to save this file
#!/usr/local/bin/perl

$baseURL = "http://www.ryerson.ca/perl/arrays";

@finalURL = split (/\//, $baseURL);

foreach $part (@finalURL){
    print "$part\n";
}

The strange looking /\// is really just an escaped forward slash. The two outer forward slashes contain the escaped forward slash: \/ which is necessary because forward slashes identify the boundaries of the pattern. Here is the output. Notice the null string where we had a double forward slash in the initial string. 
[/home/angel3/blesser/PerlWork]>split.pl
http:

www.ryerson.ca
perl
arrays
[/home/angel3/blesser/PerlWork]> |

Separating Variables in a Line of Text

Given a text file with delimited fields on each line, it may be necessary to read in each line of text, separate the fields into separate variables, and then do something with them. Here is a short example that uses variables in a list instead of splitting a string directly into a single array. First here is the data file: 
right click to save this file
123456789,Clark,Tony,M
987654322,Jones,Brian,M
234567890,Rogers,Dave,M
345678901,Peterson,Dana,F
456789012,Lewis,Diane,F
567890123,Smith,Larry,M
678901234,Tucker,Gwen,F
789012345,King,Ed,M
890123456,Anderson,Stan,M
901234567,Murphy,Stan,M
012345678,Simpson,Bart,M
098765432,Grant,Carl,M
987654321,Chan,Larry,M
876543210,Hill,Edward,M
765432109,Porter,Susan,F
654321098,Singh,Dave,M
543210987,Kay,Alexander,M
432109876,Hunter,Ann,F
321098765,Smith,Renee,F
 
right click to save this file
#!/usr/local/bin/perl

open (MASTER, "master.dat") || die ("Can't open master file. $!\n");

while ($line = <MASTER>){
   my($studentID, $lastName, $firstName, $gender) = split (/,/, $line);
   print "$firstName $lastName\'s student number is: $studentID\n";
}
It's not pretty, but here is the output: 
[/home/angel3/blesser/PerlWork]>split.pl
Tony Clark's student number is: 123456789
Brian Jones's student number is: 987654322
Dave Rogers's student number is: 234567890
Dana Peterson's student number is: 345678901
Diane Lewis's student number is: 456789012
Larry Smith's student number is: 567890123
Gwen Tucker's student number is: 678901234
Ed King's student number is: 789012345
Stan Anderson's student number is: 890123456
Stan Murphy's student number is: 901234567
Bart Simpson's student number is: 012345678
Carl Grant's student number is: 098765432
Larry Chan's student number is: 987654321
Edward Hill's student number is: 876543210
Susan Porter's student number is: 765432109
Dave Singh's student number is: 654321098
Alexander Kay's student number is: 543210987
Ann Hunter's student number is: 432109876
Renee Smith's student number is: 321098765
[/home/angel3/blesser/PerlWork]> |

Assembling the Elements of an Array into a String

 
right click to save this file
#!/usr/local/bin/perl

$baseURL = "http://www.ryerson.ca/perl/arrays";

@finalURL = split (/\//, $baseURL);

$finalURL[$#finalURL + 1] = "split.pl";

$finalURL = join("/", @finalURL);

print "$finalURL\n";
This script just appends another element on to the end of an array. (Using the push function is a more efficient way to do this.) Then the array is joined together using the join function. The output from this program: 
[/home/angel3/blesser/PerlWork]>join.pl
http://www.ryerson.ca/perl/arrays/split.pl
home/angel3/blesser/PerlWork]> |