Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the shortest way to convert string characters into an array of characters in Perl?

Tags:

perl

If I've the string "abcd", what's the shortest way to convert in @arr containing qw(a b c d)?

like image 887
AgA Avatar asked Mar 27 '11 18:03

AgA


People also ask

How do I convert a string to an array in Perl?

A string can be converted into an array using the split() function. @ARRAY = split (/REGEX/, $STRING); Where: @ARRAY is the array variable that will be assigned the resulting array.

How do I split a string into a character in Perl?

If you need to split a string into characters, you can do this: @array = split(//); After this statement executes, @array will be an array of characters. split recognizes the empty pattern as a request to make every character into a separate array element.

How do I shift a string in Perl?

Perl | shift() Function shift() function in Perl returns the first value in an array, removing it and shifting the elements of the array list to the left by one. Shift operation removes the value like pop but is taken from the start of the array instead of the end as in pop.

How do I create an empty array in Perl?

To empty an array in Perl, simply define the array to be equal to an empty array: # Here's an array containing stuff. my @stuff = ("one", "two", "three"); @stuff = (); # Now it's an empty array!


2 Answers

The simplest way is with split with a regex that matches anything.

my @arr = split //, "abcd";
like image 117
Colin Newell Avatar answered Oct 06 '22 23:10

Colin Newell


my @arr = split //, "abcd";

my @arr = "abcd" =~ /./sg;

my @arr = unpack '(a)*', 'abcd';
like image 39
ikegami Avatar answered Oct 07 '22 00:10

ikegami