Posts

Showing posts from July, 2011

Count number of repeating characters in powershell, ugly and dirty way

Recently I was asked to write a script to count the number of times each letter appears in a text file. This script ignores case sensitivity. I am working with a copy of War and Peace from Project Gutenberg. It reminded me of the MapReduce/Hadoop tutorial I'd followed on the Apache website to count the number of words in a collection of books. Here's the script, please suggest a better approach if you dare: #Artem Ervits - version 0.0.1 #this script counts a number of times each character appears in a file, case insensitive $names = Get-Content "C:\Users\are9004\Desktop\wnp.txt" $a = 0; $b = 0; $c = 0; $d = 0; $e = 0; $f = 0; $g = 0; $h = 0; $i = 0; $j = 0; $k = 0; $l = 0; $m = 0; $n = 0; $o = 0; $p = 0; $q = 0; $r = 0; $s = 0; $t = 0; $u = 0; $v = 0; $w = 0; $x = 0; $y = 0; $z = 0 foreach($name in $names) { for($i = 0; $i -lt $name.Length; $i++) { if($name[$i] -contains 'a') { $a++; }