Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

(tye)Re: Counting number of characters in a string

by tye (Sage)
on Mar 13, 2001 at 02:35 UTC ( #63996=note: print w/replies, xml ) Need Help??


in reply to Counting number of characters in a string

my $count= 0; for(0..255){ my $c= pack "C", $_; $count += () = $str =~ /\Q$c\E/g; }

Warning! This probably yields an inaccurate count for Unicode strings.

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
(tye)Re2: Counting number of characters in a string
by tye (Sage) on Mar 13, 2001 at 02:54 UTC

    I just realized that most strings won't have most characters in them so this can be sped up significantly via:

    my $count= 0; for( keys %{ { map {$_,1} unpack "C*", $str } } ){ my $c= pack "C", $_; $count += () = $str =~ /\Q$c\E/g; }
            - tye (but my friends call me "Tye")

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://63996]
help
Chatterbox?
[marioroy]: LA my @music_times = qx(find \$HOME/Music/ Albums -type f -iname \'*.mp3\' -exec mp3info -p \"%S\\n\" {} \\;);
[marioroy]: Only 2 backslash \\n
[marioroy]: LA no space before Albums
[Lady_Aleena]: Good guh! I think I will use File::Find when working in perl. That is too many toothpicks.
[marioroy]: mp3info must be in the PATH
[marioroy]: LA ++
[Lady_Aleena]: marioroy, this is forcing me to learn perl modules to use instead.

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2017-04-23 21:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (432 votes). Check out past polls.