perl - 如何从 Perl 数组中找到与第二个数组中的元素相匹配的元素?

标签 perl

我有两个数组

@one = ("1|1|the|dog|ran", "1|2|a|x|b", "2|8|e|s|e");
@two = ("1|2|a|x|b", "1|1|down|the|street", "2|8|e|s|e");

我需要用前两个“|”来匹配它们分离的元素。这样,当在 $one[0] 上时,搜索将返回 $two[1]。

每个数组中有数百万行,因此我需要最快的方法来执行此操作。

编辑: 对困惑感到抱歉。我想处理前2个“|”分隔的元素(即 1|2、2|1)作为数组的键,循环遍历第一个数组并使用该键搜索第二个数组以获取第二个数组中的值。这有帮助吗?

最佳答案

- For each record in the second array,
  - Parse the record
  - Add it to a hash keyed by the first two fields.

- For each record in the first array,
  - Parse the record
  - Look in the hash for a record with the appropriate key.
  - If there is one,
    - Do something with it.

关于perl - 如何从 Perl 数组中找到与第二个数组中的元素相匹配的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13403294/

相关文章:

python - 编写相当于 Perl 代码的 Python 代码

mysql - 如何在保存之前清理用户输入以进行正确的内容编码?

perl - 奇怪的 perl 行为与 glob

perl - 从命令行调用 perl 子例程

python - BitTorrent 跟踪器 API 不错吗?

perl - Livejournal XML-RPC 协议(protocol)不匹配

perl - 如何找到两个数组元素之间的距离?

regex - Perl,从正则表达式匹配分配给变量

PHP等同于Perl格式化函数

mysql - PERL PAR 打包程序 MYSQL libmysql_.dll