Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Split a string into its individual characters
#!/usr/bin/perl
use strict;
use warnings;
#########################################################################
# Script: characters.pl
# Author: Jefferson Kirkland
# Date: December 11, 2007
#
# Description: This script takes a phrase, with or without spaces, and
# prints it out, one character at a time with each character on its own
# line.
#
# Why does this code work? Here is the explanation from the perldoc
# page:
#
# A pattern matching the null string (not to be confused with a null
# pattern // , which is just one member of the set of patterns matching
# a null string) will split the value of EXPR into separate characters
# at each point it matches that way.
# (From: http://perldoc.perl.org/functions/split.html)
#
# Copyright: Jefferson Kirkland, ParsedContent Solutions
#########################################################################
my $phrase = "pure flipping magic";
my @array = split(/ */, $phrase);
foreach my $char (@array)
{
print("$char\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment