Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

How to count the number of chars in a string

by misterperl (Pilgrim)
on Jun 05, 2020 at 17:15 UTC ( [id://11117733]=CUFP: print w/replies, xml ) Need Help??

I needed to do this, and checked here and elsewhere for a method. Many had loops, some hashes , most multi-line.

I came up with (perhaps not exclusively, as was The Calculus!) this approch to counting the # of chars in a string.

This counts integers, for example:

my $x = () = /\d\D*/g;
The trick being /something(0-many in the set of !something)/as many as it finds..... ..so like, for letter A, <code> my $As = () = /A^A*/g

We dont need no stinking loops, hashes, etc..

Just an idear I had.. Probably some others had it to but I didn't find it in searches....

caveat- this may not work in dbg. If not, try it outside dbg and it should be OK.

Replies are listed 'Best First'.
Re: How to count the number of chars in a string
by choroba (Cardinal) on Jun 05, 2020 at 17:55 UTC
    You don't need to match the non-digits when counting digits, the regex engine will happily skip over them. So, you can shorten the line to
    my $count = () = /\d/g;

    If you need to count the characters in a tight loop, note that tr is faster than regex match:

    my $count = tr/0-9//;

    Hashes are usually used when you're interested in frequencies of the individual characters.

    my %freq; ++$freq{$_} for split //;

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: How to count the number of chars in a string
by LanX (Saint) on Jun 05, 2020 at 17:36 UTC
    You are not counting characters* but digits \d which might be followed by 0 or more non-numbers \D* .

    Not sure why?

    I'd suggest better code if I new what your goal is.

    Edit
    The following example shows how to match (and consequently count) digits or any other character class.

    No need for weird negated character classes.

    DB<6> x $_='12a3b';/\d/g 0 1 1 2 2 3 DB<7> x $_='12a3b';/[ab]/g 0 'a' 1 'b'

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    *) Character_(computing) include letters, numerical digits, common punctuation marks (such as "." or "-"), and whitespace.

    Update

    In case you are only counting single characters and not combinations consider using tr instead, like described in Re: Some odd ambiguity in this regex

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://11117733]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-16 04:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found