Ask a Question related to PERL Miscellaneous, Design and Development.
-
Researcher #1
How do I count blocks of characters in a string variable?
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.
Researcher Guest
-
Count Character In String?
I have various strings that will be returned from a query, but I can to be able to remove the space and replace them with an underscore. For... -
Special Characters in Component's String-type variable
Hi, I have a component that has a string variable which I put text in, using the parameters window... I would like very much to be able to go into... -
Count characters in Freehand
Hello Is it posible to count how many character (words, commas, ) there is in any Freehand document? (For publishing needs) Thank you.. -- -
count char in string
hy all, I am trying to find a php function that can count howmany times a specific char is in a string. for example: $string = "Hello world";... -
How to Count the Characters of a Field?
I want to discover how many times the bar (/) appears inside of the field numnota how do I do? -
Jeff 'japhy' Pinyan #2
Re: How do I count blocks of characters in a string variable?
On 6 Sep 2003, Researcher wrote:
If you just want to count the number of times a character occurs in a>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++;
> }
>}
string, use the tr/// operator.
$o_count = $string =~ tr/o//;
Read 'perldoc perlop' for more on tr///.
--
Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
"And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
Jeff 'japhy' Pinyan Guest
-
John W. Krahn #3
Re: How do I count blocks of characters in a string variable?
Jeff 'japhy' Pinyan wrote:
Or if 'o' is in a variable you can do this:>
> On 6 Sep 2003, Researcher wrote:
>>> >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///.
my $char = 'o';
$count = () = $string =~ /\Q$char/g;
John
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
Gunnar Hjalmarsson #4
Re: How do I count blocks of characters in a string variable?
Researcher wrote:
<snip>> 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",
This method works with both single characters and strings:> what if I want to count a block of characters instead of one only
> (e.g. how many "el" there are in $text)?
$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
--
Gunnar Hjalmarsson
Email: [url]http://www.gunnar.cc/cgi-bin/contact.pl[/url]
Gunnar Hjalmarsson Guest
-
Greg #5
Re: How do I count blocks of characters in a string variable?
Gunnar Hjalmarsson <noreply@gunnar.cc> wrote in message news:<bjdong$hp5o9$1@ID-184292.news.uni-berlin.de>...
Just as an alternative to the regex methods, a common way in some> Researcher wrote:>> > 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",
> <snip>
>>> > 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
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.
Greg Guest
-
Anno Siegel #6
Re: How do I count blocks of characters in a string variable?
Greg <gdsafford@hotmail.com> wrote in comp.lang.perl.misc:
Those must be languages without a hash data type.> Gunnar Hjalmarsson <noreply@gunnar.cc> wrote in message
> news:<bjdong$hp5o9$1@ID-184292.news.uni-berlin.de>...>> > Researcher wrote:> >> > > 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",
> > <snip>
> >> >> > > 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
> 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:
In Perl, there is no need to decode the characters first:> my $text = "The book is yellow";
> my @a;
>
> $a[ord(lc $_)]++ for split(//, $text);
>
> print $a[ord('o')], $/;
my %a;
$a{ $_}++ for split //, $text;
print "$a{ o}\n";
Re-introduce the lc() if wanted.
Anno
Anno Siegel Guest



Reply With Quote

