简体   繁体   English

计算来自外部文件的单词出现次数并使用Erlang显示它们?

[英]Counting word occurences from an external file and display them using Erlang?

I am having trouble fully understanding how to count how many times an element occurs within a text file. 我无法完全理解如何计算元素在文本文件中出现的次数。 I am able to read the file in like so - 我可以像这样读取文件-

-module(list3).

-export([readlines/1]).

readlines(FileName) ->
{ok, Device} = file:open(FileName, [read]),
try get_all_lines(Device)
  after file:close(Device)
end.

get_all_lines(Device) ->
case io:get_line(Device, "") of
    eof  -> [];
    Line -> Line ++ get_all_lines(Device)
end.

I would transfer the text file into a list of strings and I understand that list:length or foldl would normally count the number of elements within a list returning overall how many words there would be, however, I would like to list the words within the file and how many times it has occurred. 我会将文本文件传输到字符串列表中,并且我了解list:length或foldl通常会计算列表中元素的数量,从而返回总的单词数,但是,我想在列表中列出单词文件以及发生了多少次。 Would this be done through pattern matching? 是否可以通过模式匹配来完成? The only solution that I can currently think of would require a lot of code. 我目前能想到的唯一解决方案将需要大量代码。

Any tips will be appreciated. 任何提示将不胜感激。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM