Such as:
<script>
//code
aaaa
zzzz
ccc
</script>
Counts will be produced: a=4, z=4, c=3. The is not for web site
uploading, but just to get statistics for own research. All data will
be in plain text. Everything will happen in a single computer, no
Internet, nor ISP.
I believe it is not a difficult job for a programing language, such as
C. With the MS Word the letters can be counted one type at a time. But
that is rather slow. Maybe Javascript can do the work so efficiently
as a fully-fledged programing language. Thanks.
Dung Ping
Jim Davis - 31 Aug 2005 18:56 GMT
> Such as:
JavaScript can do this pretty easily (really any language that can navigate
a string and has the concept of a hash table or the like can do it easily).
Conversationally this might be:
+) Create a hash table (new Array()) to store the counts. The keys of this
array will be the characters in question.
+) Start looping from the first to last character of the string. Use the
String.length property to get the length and the String.charAt() method to
get the current character.
+) For each character see if there's already a hash-table entry for the
character. If there IS increment the value and move on. If there ISN'T
create an entry for the character and populate it's value with 1 (one).
+) Output your hash table in whatever format you like.
If this isn't clear let me know and I'll try to work up some code.
How you get the source string into the script might be a matter for
discussion (if you want the current script string you'd have to grab the
source from the DOM - but how you'd differentiate different scripts and such
is really something you'd have to answer).
Jim Davis
Martin Honnen - 31 Aug 2005 19:54 GMT
> +) Create a hash table (new Array()) to store the counts. The keys of this
> array will be the characters in question.
Why do you want to use
new Array()
and not
new Object()
if you want to have keys or properties which are characters?

Signature
Martin Honnen
http://JavaScript.FAQTs.com/
Jim Davis - 31 Aug 2005 20:13 GMT
>> +) Create a hash table (new Array()) to store the counts. The keys of
>> this array will be the characters in question.
[quoted text clipped - 4 lines]
> new Object()
> if you want to have keys or properties which are characters?
Either way. Six of one, half-dozen of the other.
In this context they both work exactly the same except the Array will give
you access to the array properties and methods (which you may, or may not,
want to use).
Jim Davis
Lasse Reichstein Nielsen - 31 Aug 2005 20:48 GMT
> In this context they both work exactly the same except the Array will give
> you access to the array properties and methods (which you may, or may not,
> want to use).
In this case, yes, because you only use single characters as keys. But
there *is* one problem that will bite you eventually if you use
Javascript arrays as associative arrays: You can't use "length" as a
key.
/L

Signature
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jim Davis - 31 Aug 2005 22:22 GMT
>> In this context they both work exactly the same except the Array will
>> give
[quoted text clipped - 6 lines]
> Javascript arrays as associative arrays: You can't use "length" as a
> key.
Very good point to keep in mind.
Thanks!
Jim Davis
Lee - 31 Aug 2005 21:03 GMT
Dung Ping said:
>Such as:
>
[quoted text clipped - 17 lines]
>that is rather slow. Maybe Javascript can do the work so efficiently
>as a fully-fledged programing language. Thanks.
Sure. Almost like a fully-fledged programming language.
The following isn't very efficient, but it's pretty fast,
and it took under 10 minutes to write and debug.
Save this to your Windows desktop as "count.hta" and
double-click the icon:
<html>
<head>
<title>Letter Frequency</title>
<hta:application id="lcount" applicationname="Letter Frequency">
<script type="text/javascript">
function countFile(pathName) {
var tally=new Object();
var fso=new ActiveXObject("Scripting.FileSystemObject");
var f=fso.OpenTextFile(pathName,1,false); //readonly, do not create
while(!f.AtEndOfStream) {
var c=f.read(1);
tally[c]=(tally[c]|0)+1;
}
f.Close();
var res="<xmp>";
for(var uc=65;uc<91;uc++) {
res+=String.fromCharCode(uc)+": "
+(tally[String.fromCharCode(uc)]|0
+ tally[String.fromCharCode(uc+32)]|0)+"\n";
}
res+="</xmp>";
document.getElementById("results").innerHTML=res;
}
</script>
</head>
<body>
<form>
Choose a file:
<input type="file" name="path" size="60">
<input type="button" value="Count" onclick="countFile(this.form.path.value)">
</form>
<div id="results"></div>
</body>
</html>
Dung Ping - 31 Aug 2005 22:40 GMT
I want to thank all for your help. I have saved all your messages, and
will study them very carefully. At this moment I still don't
understand many of the tags and procedures.
Thanks a million again.