R
Researcher
I'm trying to create a code to count a certain character in a string,
lets say that I want to know how many letters "o" there are in the
text "The book is yellow", I'd use the following code:
$text = "The book is yellow";
@characters = split (//,$text);
foreach $char (@characters){
if ($char eq "o"){
$count++;
}
}
Very easy, but I think this is a lot of code for such a simple task,
there's got to be a simpler way to do this, besides, what if I want to
count a block of characters instead of one only (e.g. how many "el"
there are in $text)? How do I do this?
I hope someone can help me with this.
lets say that I want to know how many letters "o" there are in the
text "The book is yellow", I'd use the following code:
$text = "The book is yellow";
@characters = split (//,$text);
foreach $char (@characters){
if ($char eq "o"){
$count++;
}
}
Very easy, but I think this is a lot of code for such a simple task,
there's got to be a simpler way to do this, besides, what if I want to
count a block of characters instead of one only (e.g. how many "el"
there are in $text)? How do I do this?
I hope someone can help me with this.