Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I split a word into its component letters?

Tags:

perl

I am working with Perl and I have an array with only one word:

@example = ("helloword")

I want to generate another array in which each element is a letter from the word:

@example2 = ("h", "e", "l"...)

I need to do that because I need to count the numbers of "h", "e"... How can I do this?

like image 830
Mia Lua Avatar asked Dec 09 '22 01:12

Mia Lua


1 Answers

To count how many times letter occurred in a string,

print "helloword" =~ tr/h//; # for 'h' letter

otherwise you can split string and assign list to an array,

my @example2 = split //, $example[0];
like image 73
mpapec Avatar answered Dec 11 '22 08:12

mpapec