How do I count blocks of characters in a string variable?

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.
 
J

Jeff 'japhy' Pinyan

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++;
}
}

If you just want to count the number of times a character occurs in a
string, use the tr/// operator.

$o_count = $string =~ tr/o//;

Read 'perldoc perlop' for more on tr///.
 
J

John W. Krahn

Jeff said:
If you just want to count the number of times a character occurs in a
string, use the tr/// operator.

$o_count = $string =~ tr/o//;

Read 'perldoc perlop' for more on tr///.

Or if 'o' is in a variable you can do this:

my $char = 'o';

$count = () = $string =~ /\Q$char/g;


John
 
G

Gunnar Hjalmarsson

Researcher said:
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",

what if I want to count a block of characters instead of one only
(e.g. how many "el" there are in $text)?

This method works with both single characters and strings:

$text = "The book is yellow";
$count++ while $text =~ /o/g;

Btw, you asked a FAQ. This is where you should have looked up the
answer:

perldoc -q count
 
G

Greg

Gunnar Hjalmarsson said:
This method works with both single characters and strings:

$text = "The book is yellow";
$count++ while $text =~ /o/g;

Btw, you asked a FAQ. This is where you should have looked up the
answer:

perldoc -q count

Just as an alternative to the regex methods, a common way in some
languages of counting all the character types in a string would be to
use the character code as an array index:

my $text = "The book is yellow";
my @a;

$a[ord(lc $_)]++ for split(//, $text);

print $a[ord('o')], $/;


Not an expert, so FWIW.
 
A

Anno Siegel

Greg said:
Just as an alternative to the regex methods, a common way in some
languages of counting all the character types in a string would be to
use the character code as an array index:

Those must be languages without a hash data type.
my $text = "The book is yellow";
my @a;

$a[ord(lc $_)]++ for split(//, $text);

print $a[ord('o')], $/;

In Perl, there is no need to decode the characters first:

my %a;
$a{ $_}++ for split //, $text;

print "$a{ o}\n";

Re-introduce the lc() if wanted.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top