Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Counting number of characters in a string

by simul (Novice)
on Jan 23, 2009 at 21:46 UTC ( [id://738601]=note: print w/replies, xml ) Need Help??


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

$cnt = @{[$str =~ /(\.)/g]};

Replies are listed 'Best First'.
Re^3: Counting number of characters in a string
by toolic (Bishop) on Jan 23, 2009 at 22:00 UTC
    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

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

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

      Cheaper alternative:

      my $cnt =()= $str =~ /./sg;
      Use the length() function.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-03-29 01:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found