Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
There's more than one way to do things
 
PerlMonks  

count words in a line

by benlaw (Scribe)
on Jun 06, 2002 at 11:47 UTC ( [id://172257]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

benlaw has asked for the wisdom of the Perl Monks concerning the following question:

hi monks,
simply like text:

perlmonk perlmonk perlmonk
i 'd like to count whether how many 'perlmonk' present in a line.

but if the text file change to
perl m onk p erlmonk per lm onk

is it possible too???

Replies are listed 'Best First'.
Re: count words in a line
by tadman (Prior) on Jun 06, 2002 at 12:01 UTC
    Question A: How many words are there on a line?
    my $count = () = /\w+/g;
    Question B: How many 'perlmonk' words are there on a line?
    my $count = () = /\bperlmonk\b/g;
    Question C: How many "perlmonk" spaced words are there on a line?
    my $regex = join ('\s*', split(//, 'perlmonk')); my $count = () = /$regex/g;
    In the future, please use more capitals and less superfluous punctuation. These postings, as I'm sure you're well aware, are archived for future reference. A small effort to maintain some level of quality helps everyone. As jeffa says in How (Not) To Ask A Question, the shift keys are there for a reason.
Re: count words in a line
by marvell (Pilgrim) on Jun 06, 2002 at 12:02 UTC

    We build the regex from the word using \s* in between the letters.

    my $word = "perlmonk"; my $re = join('\s*',$word =~ /\w/g); my $qre = qr/$re/; while (<DATA>) { my $count = () = /$qre/g; print "$count == $_"; } __DATA__ perlmonk perlmonk perlmonk perl m onk p erlmonk per lm onk per lm onk

    Updated: actually answered question this time.

    --
    Steve Marvell

Re: count words in a line
by Anonymous Monk on Jun 06, 2002 at 12:12 UTC
    Sure you can. If your data is nice and normal like in the first example you can do something like this.
    @sentence = split /\s/,"perlmonk perlmonk perlmonk "; $numWords = @sentence;

    to count the number of occurrences of perlmonk and not other words you could use the same idea but with a regular expression. I will leave it to you to figure it out. This is a clunky way to do it....
    @sentence = split /\s/,"perlmonk perlmonk perlmonk and some other wor +ds that are not perlmonk"; foreach $word (@sentence) { if ( $word =~/perlmonk/) { $numOccur++; } }
    The last one would be more tricky, but it can be done with a fairly easy regxp... something along the lines of  /p\s*e\s*..... should do the trick
    Good luck.
    Disclamer: all code untested and off the top of my head.
Re: count words in a line
by jmcnamara (Monsignor) on Jun 06, 2002 at 12:23 UTC

    To match arbitrary words use \w+ instead of "perlmonk":
    #!/usr/bin/perl -wl use strict; my $line1 = "perlmonk perlmonk perlmonk"; my $count1 = () = $line1 =~ /perlmonk/g; my $line2 = "perl m onk p erlmonk per lm onk"; # Remove the spaces and count as before $line2 =~ s/ //g; my $count2 = () = $line2 =~ /perlmonk/g; print for $count1, $count2; __END__ Prints: 3 3

    --
    John.

Re: count words in a line
by Aristotle (Chancellor) on Jun 06, 2002 at 15:42 UTC
    Even the last one is simpler than the others here would suggest. You can simply remove all whitespace first: $line =~ s/\s+//g; After that, you can count the occurences with any of the other methods proposed here.

    Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://172257]
Approved by wil
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.