We can transform a string to an array in Perl the split()
method.
It only takes two parameters to start the transformation.
split(at, str)
at
: This specifies where to start the transformation.
str
: This is the string we want to transform to an array.
The value returned is an array.
# create some strings$name = "Okwudili Onyejiaku";$quote = "Edpresso-is-great!";# transform strings to array@arr1 = split(" ", $name);@arr2 = split("-", $quote);# print results for @arr1print "First Array:\n\n";for (my $index = 0; $index <= $#arr1; $index++) {print("Element $index is $arr1[$index]\n");}# print results for @arr2print "\nSecond Array:\n\n";for (my $index = 0; $index <= $#arr2; $index++) {print("Element $index is $arr2[$index]\n");}
$name
to an array at space (" "
) and store the result in array @arr1
.$quote
to an array at hyphen ("-"
) and store the result in array @arr2
.