CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Count name! Help me!

 



thesun
Novice

May 15, 2011, 10:28 AM

Post #1 of 5 (3480 views)
Count name! Help me! Can't Post

I' ve a problem about counting name in a string.
Name has format {#name} such as {#john} or {#mr_peter}... A name include lowercase character, digits or underline "_" character is valid.

And, enter a paragraph, how many name in this.
Exam:
Input: {#john} and {#conankudo} play PES with {#mr_invincible}. { {#john} win {###} {
Output:
4
because have 4 name is: {#john}, {#conankudo}, {#mr_invincible} and {#john}.

Who can help me! please.


BillKSmith
Veteran

May 15, 2011, 5:29 PM

Post #2 of 5 (3471 views)
Re: [thesun] Count name! Help me! [In reply to] Can't Post


Code
use strict; 
use warnings;
my $paragraph =
'{#john} and {#conankudo} play PES with {#mr_invincible}. { {#john} win {###} {'
;
print "Input: " . $paragraph . "\n";
(my @names) = $paragraph =~ m/(\{#[a-z_0=9]+\})/g;
print "Output: " , scalar @names, "\n";

Good Luck,
Bill


shawnhcorey
Enthusiast


May 15, 2011, 6:41 PM

Post #3 of 5 (3469 views)
Re: [BillKSmith] Count name! Help me! [In reply to] Can't Post


In Reply To

Code
(my @names) = $paragraph =~ m/(\{#[a-z_0=9]+\})/g;



Make that second equal sign a minus:

Code
(my @names) = $paragraph =~ m/(\{#[a-z_0-9]+\})/g;


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


thesun
Novice

May 15, 2011, 7:51 PM

Post #4 of 5 (3466 views)
Re: Count name! Help me! [In reply to] Can't Post

thanks everyone.
I want to enter paragraph in multiline.
It is my code:

Code
$a=0; 
while($para=<>){
@name=$para=~ m/(\{#[a-z_0=9]+\})/g;
$a+=scalar@name;
if(eof){print $a,"\n";}
}

But it not stop. I don't understands


BillKSmith
Veteran

May 16, 2011, 6:14 AM

Post #5 of 5 (3448 views)
Re: [thesun] Count name! Help me! [In reply to] Can't Post

Not only is your test of eof unnecessary, it probably does not do what you think (Refer perldoc -f eof).

You must correct my typo as Shawn pointed out.

This program will accept input from STDIN (usually the keyboard) until it reads an end-of-file. The way you type an end-of-file depends on your operating system. (For windows, use CTRL-Z followed by ENTER.)

Although it has nothing to do with you current problem, you should always use the pragmas "use strict" and "use warnings". This will force you to declare all your variables (almost always with "my").
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives