How to read a string into an array of characters using split

Perl’s split function has a useful feature that will split a string into characters. This works by supplying an empty regex pattern (“//”) to the split function. This can be used to easily split a word into an array of letters, for example:

my $word = 'camel';
my @letters = split(//, $word);

Perl’s official documentation has more on the split function. You can read it online or by running the following command at the terminal:

perldoc -f split

Tags

David Farrell

David is the founder and editor of PerlTricks.com. An organizer of the New York Perl Meetup, he works for ZipRecruiter as a software developer.

Browse their articles