search instagram arrow-down

Archives

Categories

Meta

Processing string character by character:

A very useful function in Perl is split, which splits up a string and places it into an array. The function uses a regular expression and as usual works on the $_ variable unless otherwise specified. The split function is used like this:
$info = “Caine:Michael:Actor:14, Leafy Drive”; @personal = split(/:/, $info);
which has the same overall effect as
@personal = (“Caine”, “Michael”, “Actor”, “14, Leafy Drive”);
If we have the information stored in the $_ variable then we can just use this instead
@personal = split(/:/);
If the fields are divided by any number of colons then we can use the RE codes to get round this. The code
$_ = “Capes:Geoff::Shot putter:::Big Avenue”; @personal = split(/:+/);
is the same as
@personal = (“Capes”, “Geoff”,”Shot putter”, “Big Avenue”);
But this:
$_ = “Capes:Geoff::Shot putter:::Big Avenue”; @personal = split(/:/);
would be like
@personal = (“Capes”, “Geoff”, “”, “Shot putter”, “”, “”, “Big Avenue”);
A word can be split into characters, a sentence split into words and a paragraph split into sentences:
@chars = split(//, $word); @words = split(/ /, $sentence); @sentences = split(/\./, $paragraph);
In the first case the null string is matched between each character, and that is why the @chars array is an array of characters – ie an array of strings of length 1.
Ref:http://www.comp.leeds.ac.uk/Perl/split.html

This entry was posted in Perl.
Leave a Reply
Your email address will not be published. Required fields are marked *

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: