Re: Count Function?

2003-06-27 Thread Kevin Pfeiffer
Hi Paul,

In article <[EMAIL PROTECTED]>, Paul 
Johnson wrote:

> 
> Kevin Pfeiffer said:
[...]
>> But what I can't figure out (and have tried several variants) is how to
>> get the count when using a variable (ala' from inside an eval). This is
>> the closet I got:
>>
>> my $sentence = "Here is my test sentence.\n";
>> my $letter = 'e';
>> my $count;
>>
>> eval {$count = $sentence =~ tr/$letter//};
>> die $@ if $@;
>>
>> print "The letter $letter appears $count times in the sentence...";
>>
>> It produces "The letter e appears 10 times..." but the answer should be
>> "6".
>> :-(
> 
> You didn't follow the example - you changed the quotes for the eval.
> 
> You need this:
> 
>   eval "\$count = \$sentence =~ tr/$letter//";
> 
> or even better:
> 
>   $count = eval "\$sentence =~ tr/$letter//";

mumble, mumble...  :-)  I did try the example first, exactly has given in 
perlop, but it didn't work. What I didn't try was the escape you use before 
the string symbol.

> Read up on the two different types of eval, then you should be able to
> find out why you got 10.

Will do! It sounds like this might be a case of not having read far enough 
(and I distantly remember having once read about two types of eval, but it 
apparently hasn't sunken in, yet). Thanks for the tips.

-K
-- 
Kevin Pfeiffer
International University Bremen

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Count Function?

2003-06-27 Thread Paul Johnson

Kevin Pfeiffer said:

> Thanks to Sudarshan & Janek!
>
> I found this as suggested...
>
> # NOTE: (from perlop)
> # Because the transliteration table is built at com­
> # pile time, neither the SEARCHLIST nor the REPLACE­
> # MENTLIST are subjected to double quote interpola­
> # tion.  That means that if you want to use vari­
> # ables, you must use an eval():
> #
> # eval "tr/$oldlist/$newlist/";
> # die $@ if $@;
> #
> # eval "tr/$oldlist/$newlist/, 1" or die $@;
>
> But what I can't figure out (and have tried several variants) is how to
> get the count when using a variable (ala' from inside an eval). This is the
> closet I got:
>
> my $sentence = "Here is my test sentence.\n";
> my $letter = 'e';
> my $count;
>
> eval {$count = $sentence =~ tr/$letter//};
> die $@ if $@;
>
> print "The letter $letter appears $count times in the sentence...";
>
> It produces "The letter e appears 10 times..." but the answer should be
> "6".
> :-(

You didn't follow the example - you changed the quotes for the eval.

You need this:

  eval "\$count = \$sentence =~ tr/$letter//";

or even better:

  $count = eval "\$sentence =~ tr/$letter//";

Read up on the two different types of eval, then you should be able to
find out why you got 10.

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Count Function?

2003-06-27 Thread Kevin Pfeiffer
In article <[EMAIL PROTECTED]>, Sudarshan Raghavan 
wrote:

> Nelson Ray wrote:
> 
>>Does anyone know of any sort of a function or method in perl that returns
>>the number of times a search string exists in a scalar.  Say, how many
>>"a's"
>>are there in this sentence?  I am able to write it myself, but I was
>>wondering if Perl had an inherent function for cleaner operation.  I tried
>>looking through the list of functions at www.perldoc.com without success.
>>Thanks a lot for any help.
>>
> 
> tr/// is what you need, perldoc perlop
> Assuming your string is in $_, the number of a's will be
> my $acnt = tr/a//;

Thanks to Sudarshan & Janek!

I found this as suggested...

# NOTE: (from perlop)
# Because the transliteration table is built at com­
# pile time, neither the SEARCHLIST nor the REPLACE­
# MENTLIST are subjected to double quote interpola­
# tion.  That means that if you want to use vari­
# ables, you must use an eval():
#
# eval "tr/$oldlist/$newlist/";
# die $@ if $@;
#
# eval "tr/$oldlist/$newlist/, 1" or die $@;

But what I can't figure out (and have tried several variants) is how to get 
the count when using a variable (ala' from inside an eval). This is the 
closet I got:

my $sentence = "Here is my test sentence.\n";
my $letter = 'e';
my $count;

eval {$count = $sentence =~ tr/$letter//};
die $@ if $@;

print "The letter $letter appears $count times in the sentence...";

It produces "The letter e appears 10 times..." but the answer should be "6". 
:-(













-- 
Kevin Pfeiffer
International University Bremen

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Count Function?

2003-06-27 Thread Janek Schleicher
Nelson Ray wrote at Thu, 26 Jun 2003 19:25:37 -0700:

> Does anyone know of any sort of a function or method in perl that returns
> the number of times a search string exists in a scalar.  Say, how many "a's"
> are there in this sentence?  I am able to write it myself, but I was
> wondering if Perl had an inherent function for cleaner operation.  I tried
> looking through the list of functions at www.perldoc.com without success.
> Thanks a lot for any help.

perldoc -q "How can I count the number of occurrences of a substring"


Greetings,
Janek

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Count Function?

2003-06-27 Thread Sudarshan Raghavan
Nelson Ray wrote:

Does anyone know of any sort of a function or method in perl that returns
the number of times a search string exists in a scalar.  Say, how many "a's"
are there in this sentence?  I am able to write it myself, but I was
wondering if Perl had an inherent function for cleaner operation.  I tried
looking through the list of functions at www.perldoc.com without success.
Thanks a lot for any help.
tr/// is what you need, perldoc perlop
Assuming your string is in $_, the number of a's will be
my $acnt = tr/a//;


 



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Count Function?

2003-06-27 Thread Nelson Ray
Does anyone know of any sort of a function or method in perl that returns
the number of times a search string exists in a scalar.  Say, how many "a's"
are there in this sentence?  I am able to write it myself, but I was
wondering if Perl had an inherent function for cleaner operation.  I tried
looking through the list of functions at www.perldoc.com without success.
Thanks a lot for any help.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]