Letter frequency: Difference between revisions
Content added Content deleted
(jq) |
|||
Line 1,007: | Line 1,007: | ||
return freqs; |
return freqs; |
||
}</lang> |
}</lang> |
||
=={{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> |
|||
# Input: an array |
|||
# Output: an array of [item, count] for each run |
|||
def runs: |
|||
reduce .[] as $item |
|||
( []; |
|||
if . == [] then [ [ $item, 1] ] |
|||
else .[-1] as $last |
|||
| if $last[0] == $item |
|||
then (.[0:length-1] + [ [$item, $last[1] + 1] ] ) |
|||
else . + [[$item, 1]] |
|||
end |
|||
end ) ; |
|||
explode | sort | runs | map( [ ( [.[0]] | implode) , .[1] ] ) | .[]</lang> |
|||
Example:<lang sh>jq -s -R -c -f Letter_frequency.jq somefile.txt</lang> |
|||
{{Out}} |
|||
<pre>["\n",12] |
|||
[" ",124] |
|||
["#",1] |
|||
["$",8] |
|||
["(",4] |
|||
[")",4] |
|||
["+",3] |
|||
[",",4] |
|||
["-",4] |
|||
[".",9] |
|||
["0",3] |
|||
["1",7] |
|||
[":",2] |
|||
[";",2] |
|||
["=",4] |
|||
...</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |