Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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++; } }

Replies are listed 'Best First'.
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 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 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);
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://674462]
Approved by pc88mxer
[stonecolddevin]: damn where's a working cblast? looks like several of them are out of commission
[erix]: Tanktalus home node has one
[stonecolddevin]: erix thanks mate
[stonecolddevin]: also lol @ "I stomped a crap"
[stonecolddevin]: Disciplus i was just amazed he's returned after the thorough stomp that BrowserUk and several other monks gave him for spreading crap info
[stonecolddevin]: i will admit i find the responses to his threads incredibly entertaining
[stonecolddevin]: erix what is this wizardy? there's no sound for me :-(

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2017-06-22 20:46 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (529 votes). Check out past polls.