Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^3: Counting number of characters in a string

by toolic (Bishop)
on Jan 23, 2009 at 22:00 UTC ( #738602=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Counting number of characters in a string
in thread Counting number of characters in a string

This does not work for me. I expect the length of the string 'abc' to be 3, but your code returns 0:

use strict; use warnings; my $str = 'abc'; my $cnt = @{[$str =~ /(\.)/g]}; print "$cnt\n"; __END__ 0


Comment on Re^3: Counting number of characters in a string
Download Code
Replies are listed 'Best First'.
Re^4: Counting number of characters in a string
by ikegami (Pope) on Jan 23, 2009 at 22:34 UTC

    Remove the backslash, add the "s" modifier. And the parens aren't necessary.

    my $cnt = @{[$str =~ /./sg]};

    Cheaper alternative:

    my $cnt =()= $str =~ /./sg;
Re^4: Counting number of characters in a string
by levonbarker (Initiate) on Dec 30, 2010 at 17:09 UTC
    Use the length() function.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (15)
As of 2015-07-31 13:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (278 votes), past polls