开发者

Nested foreach loop in perl only looping once

I've written a perl script that opens two files which contain lists. I want to find items in the first list that are not in the second list. The script uses two foreach loops. The outer loop goes through each line of the first list, extracting th开发者_高级运维e necessary item information. The inner loop goes through the second list, extracting the item information, then comparing that information to the item in the first list.

So, the idea is that, for each item in the first list, the script will loop through all items in the second list, looking for matches. The trouble is that the inner foreach loop only loops once. I had this same problem in PHP when looping through MySQL tables in nested while loops. The solution was to reset the index of the mysql data using mysql_data_seek for each iteration of the outer loop. How can I do this in perl with filehandles?


If your inner loop is a filehandle iterator, then you will need to reset it (say, by closing and reopening the file) every time you reach it.

foreach my $outer (@outer) {
   open INNER, '<', $inner_file;   # <--- need to add this
   while (my $inner = <INNER>) {
      ...
   }
   close INNER;                    # <--- optional with global scope filehandle
}

Alternatively, if you can spare the memory, you could copy the filehandle output to an array outside of the loop and then iterate over the array.

open INNER, '<', $inner_file;
my @INNER = <INNER>;
close INNER;

foreach my $outer (@outer) {
    foreach my $inner (@INNER) {
       ...
    }
}


It should be noted that the code as you describe it sounds very inefficient, O(n.m). You can get O(n+m) efficiency by putting the relevant contents of one file into a hash and then iterating the other file only once.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜