Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Perl Monk, Perl Meditation
 
PerlMonks  

Counting in regular expressions

by Anonymous Monk
on Mar 16, 2008 at 15:46 UTC ( #674462=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Perl newbie here. Can someone please help me with this? I want to write a perl script to count the number of digits, characters, whitespace characters and words in a string. I feel lost. I've tried writing a while loop to do this, but I end up with an infinite loop.
while($input =~ m/(\w+)/g) { if( $input =~ m/[\w\s]/g) { $count++; } }

Comment on Counting in regular expressions
Download Code
Re: Counting in regular expressions
by pc88mxer (Vicar) on Mar 16, 2008 at 16:06 UTC
    Here's a very straight-forward way to do this:
    my $digit_count = ($input =~ tr/[0-9]//); my $white_count; while ($input =~ m/\s/g) { $white_count++; } # note: can't use tr/\s// my $word_count; while ($input =~ m/\w+/g) { $word_count++; }
    As is generally the case with perl, there are many ways to perform these tasks.
Re: Counting in regular expressions
by wade (Pilgrim) on Mar 16, 2008 at 16:14 UTC
    If you want to use a regular expression, I believe that you're going to have to consume the original string, ala:
    use strict; use warnings; my $count; # get $input from some place $input =~ s/^\s+//; # dump leading whitespace while ($input =~ s/[^\s]+\s+//) { ++$count; } ++$count if ($input =~ /./); # catch the last word
    but, I think, what you really want is 'split':
    my @array = split /\s+/, $input; $count = scalar (@array);
    --
    Wade
Re: Counting in regular expressions
by McDarren (Abbot) on Mar 16, 2008 at 16:18 UTC
    Do you want separate counts of each, or just one big count?

    Anyway, when you use the /g modifier with a pattern match, you can capture all of the matches into a list, eg:

    my @digits = ($input =~ m/\d/g);
    And then the count you are after is simply the number of items in the list:
    print scalar @digits;
    Rinse, lather, repeat, etc...

    Hope this helps,
    Darren :)

Re: Counting in regular expressions
by Anonymous Monk on Mar 16, 2008 at 16:44 UTC
    Thank you for the replies. It was all helpful.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://674462]
Approved by pc88mxer
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2014-04-23 21:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (555 votes), past polls