Letter frequency: Difference between revisions

Content added Content deleted
(→‎{{header|jq}}: use an object as a hash)
Line 1,010:
=={{header|jq}}==
The following program will report the frequency of all characters in the input file, including newlines, returns, etc, provided the file will fit in memory.<lang jq>
# The input isInput: an array of int;strings.
# This function uses an object as a hash table for integers.
# the output isOutput: an object with the imploded valuesstrings as keys,
# The input is an array of int;
# the output is an object with the imploded values as keys,
# the values of which are the corresponding frequencies.
def countcounter:
| reduce .[] as $item ( {}; .[$item] += 1 ) ;
map( [.] | implode )
| reduce .[] as $item ( {}; .[$item] += 1 ) ;
 
# For neatness we sort the keys:
explode | count |map( [.] as| $countsimplode | keys) | sort[]counter | [., as $counts[.] ]counter
| keys | sort[] | [., $counter[.] ]
 
</lang>
Example:<lang sh>jq -s -R -c -f Letter_frequency.jq somefile.txt</lang>