<?xml version="1.0" encoding="windows-1252"?>
<node id="22596" title="(jjhorner) Counting Characters" created="2000-07-14 17:20:27" updated="2005-08-08 22:00:14">
<type id="11">
note</type>
<author id="6727">
jjhorner</author>
<data>
<field name="doctext">
&lt;p&gt;One way to count the occurrence of each character in a string:

&lt;code&gt;
#!/usr/bin/perl -w

use strict;  #never, never leave home without it

my %chars = ();
my $string = "J. J. is cool!";
foreach my $character (split //, $string) {
     $chars{$character}++;
}
print "Report:\n";
foreach my $key (keys %chars) {
     print "\t$key = $chars{$key}\n";
}
&lt;/code&gt;
Yields:
&lt;code&gt;
[13:22:15 jhorner@gateway scripts]$ ./20000714-1.pl 
Report:
         = 3
       ! = 1
       o = 2
       i = 1
       J = 2
       c = 1
       s = 1
       l = 1
       . = 2
&lt;/code&gt;
&lt;pre&gt;
J. J. Horner
Linux, Perl, Apache, Stronghold, Unix
jhorner@knoxlug.org http://www.knoxlug.org/
&lt;/pre&gt;</field>
<field name="root_node">
22508</field>
<field name="parent_node">
22508</field>
</data>
</node>
