Skip to content

Instantly share code, notes, and snippets.

@numberwhun
Created November 28, 2011 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save numberwhun/1399241 to your computer and use it in GitHub Desktop.
Save numberwhun/1399241 to your computer and use it in GitHub Desktop.
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