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


This article was originally posted on PerlTricks.com.

Tags

David Farrell

David is the editor of Perl.com. An organizer of the New York Perl Meetup, he works for ZipRecruiter as a software developer, and sometimes tweets about Perl and Open Source.

Browse their articles

Feedback

Something wrong with this article? Help us out by opening an issue or pull request on GitHub