2015-11-06 48 views
-3

结果,我有2个文件(大尺寸)查找匹配与PHP或Perl

  • name1.txt
  • name2.txt

name1.txt:

smooth - temper 
left - corner 
etc. 

name 2.txt:

corner = hard 
temper = small 
etc. 

我怎样才能找到name1.txt比赛中name2.txt,比内name3.txt

输出name3.txt放置:

smooth - temper = small 
left - corner = hard 
+2

你有试过什么吗? – jycr753

+0

已尝试使用perl打开reguler,如:open(FILES,$ tar)或print(“Can not Locate:$ tar \ n”); my @ lists = ; close(FILES); foreach $ list(@lists){ $ list =〜s/^ \ s + //; $ list =〜s/\ s + $ //; if($ list =〜/(.*):(.*)/ g){$ name1 = $ 1; my $ name2 = $ 2; ...但不知道如何打开第二个文件..读取它1 1直到得到匹配...它保持从文件1读取1结果..sorry我是新的程序的东西 – user3869115

+0

你应该编辑您的问题包括该代码,以便它看起来像你自己努力解决这个问题。另外,哈希。 –

回答

0

我不知道下面将如何很好地执行大文件 - 测试文件与示例完全一样,但只要我正确解释模式,就会返回所需的结果。

 $names_1=__DIR__.'\names1.txt'; 
     $names_2=__DIR__.'\names2.txt'; 
     /* Read each source file into an array */ 
     $lines_1=file($names_1, FILE_TEXT | FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES); 
     $lines_2=file($names_2, FILE_TEXT | FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES); 

     /* placeholders */ 
     $arr_1=array(); 
     $arr_2=array(); 
     $arr_3=array(); 
     $arr_4=array(); 

     /* Populate $arr_1 using value as key */ 
     foreach($lines_1 as $index => $line){ 
      list($w1,$w2)=explode('-', str_replace(' ', '', $line)); 
      $arr_1[ $w2 ]=$w1; 
     } 

     /* populate arr_2 */ 
     foreach($lines_2 as $index => $line){ 
      list($w1,$w2)=explode('=', str_replace(' ', '', $line)); 
      $arr_2[ $w1 ]=$w2; 
     } 

     /* Find where there are common keys between the two arrays */ 
     $arr_3=array_intersect_key($arr_1, $arr_2); 

     /* populate output array in format expected */ 
     foreach($arr_3 as $i => $v){ 
      $arr_4[]=$arr_1[ $i ]. ' - '. $i .' = '.$arr_2[ $i ]; 
     } 

     /* show results */ 
     echo '<pre>',print_r($arr_4,true),'</pre>';