Letter frequency: Difference between revisions
Content added Content deleted
(→{{header|jq}}: use an object as a hash) |
(→{{header|jq}}: tidy) |
||
Line 1,010: | Line 1,010: | ||
=={{header|jq}}== |
=={{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 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> |
||
⚫ | |||
# This function uses an object as a hash table for integers. |
|||
⚫ | |||
⚫ | |||
⚫ | |||
# the values of which are the corresponding frequencies. |
# the values of which are the corresponding frequencies. |
||
def |
def counter: |
||
⚫ | |||
map( [.] | implode ) |
|||
⚫ | |||
# For neatness we sort the keys: |
# For neatness we sort the keys: |
||
explode | |
explode | map( [.] | implode ) | counter | . as $counter |
||
| keys | sort[] | [., $counter[.] ] |
|||
</lang> |
</lang> |
||
Example:<lang sh>jq -s -R -c -f Letter_frequency.jq somefile.txt</lang> |
Example:<lang sh>jq -s -R -c -f Letter_frequency.jq somefile.txt</lang> |