Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以用Ruby在一个很大的txt文件中搜索字符串吗?_Ruby_String_File_Search - Fatal编程技术网

我可以用Ruby在一个很大的txt文件中搜索字符串吗?

我可以用Ruby在一个很大的txt文件中搜索字符串吗?,ruby,string,file,search,Ruby,String,File,Search,我遇到了一个问题,我找不到解决它的好办法 问题描述: File 1: short_map.txt, contains with over 2millon lines with each line consist of a short url like the one in twitter and its corresponding full web url. 例如。 例如 我想在html_index.txt文件中获得每个web url对应的短url,并将其输出到一个新的txt文件中 我的方

我遇到了一个问题,我找不到解决它的好办法

问题描述:

File 1: short_map.txt, contains with over 2millon lines with each line consist of a short url like the one in twitter and its corresponding full web url. 
例如。

例如

我想在html_index.txt文件中获得每个web url对应的短url,并将其输出到一个新的txt文件中

我的方法是阅读html_index.txt的每一行,然后将其与short_map.txt中的每一行进行比较,这样我就可以得到我想要的一切。问题是:太慢了

有人能帮我找到一种更快的算法吗


问题解决:使用哈希表将工作,请参考第一个答案!谢谢

将short_map.txt文件内容读入一个散列,其中键中为长url,相应的短url为其值。当您想要检索一个短url时,您可以只进行哈希查找,这非常快

我完全按照你说的做了,它工作得很好。谢谢请把这个问题标为已回答。
File 2: html_index.txt, contains with about 50k lines with each line stands for a full web url.