Tuesday, April 1, 2008

Perl string to character array conversion

Share at Facebook

Below is an example code that will split a string into an array of char[] like C/C++ or other language. There is no built in function in Perl to do such task. Hope below example will helpful for many people.

## String to convert into char array
my $str = "my name is deadman";

## Spliting the string with no delimeter.
my @char_array = split(//,"$str");

foreach my $char (@char_array){
   print $char . "\n"


Randal L. Schwartz said...

What do you mean "not built in"? That looks pretty built in to me!

Also, it's a rare operation, because after all, the regex stuff wants strings, not chars. Not much of Perl deals with chars.

Wolf said...

The split function is of course a built in function of Perl. But not like in Java toCharArray() that only converts string into a char[].

Wolf said...

Here is another post perl-convert-character-array-to-string. If anyone requires, can take a look.

muffin said...