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

时间:2012-11-15 17:51:51

标签: 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");

我需要通过前两个匹配它们“|”分开的元素。因此当$ 1 [0]时,搜索将返回$ 2 [1]。

每个阵列中有数百万行,所以我需要以最快的方式执行此操作。

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

1 个答案:

答案 0 :(得分:4)

- 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.