2017-05-14 4 views
0

私はファイルsay-list.txtから読み込み、list.txtの文字列がディレクトリ内の複数のファイルに存在するかどうかを確認しています。 (E:\ work ...)。以下は私のコードです。期待通りに動作します。ディレクトリ内のファイルのいずれかからクラスの\ 234ヘクタールとキャップの\ 7y6t5 - Rubyアクセス配列の値

pool = '' 
rows = Array[] 
id_files = Dir.glob("E:\work'*-access.txt") 

value=File.open('E:\nando\list.txt').read 
value.each_line do |line| 
    line.chomp! 
    id_files.each do |file_name| 
     text = File.read(file_name) 
     new_content = text.gsub(/#{Regexp.escape(line)}\,\s/, '').gsub(/#{Regexp.escape(line)}/, '') 

     unless text == new_content 

     text.each_line do |li| 
      if li.match(/#{Regexp.escape(line)}/) then 
      rows << li # I didn't complete the array part 
      puts rows[0] 
      puts rows[1] 
      pool = li.split(" ")[0] 
      end 
     end 
     File.open('E:\Removed_users.txt', 'a') { |log| 
     log.puts "Removed from: #{file_name}" 
     log.puts "Removed id : #{line}" 
     log.puts "Removed from row :#{pool}" 
     log.puts "*" * 50 
     } 
     File.open(file_name, "w") { |file| file.puts new_content } 
     end 
    end 
end 

私は文字列をfile.Egをログに以下の形式で印刷するには、コードを強化しようとしています。

Removed class\234ha from row = id and dept 
Removed cap\7y6t5 from row = id 

E:\作業\サンプル-access.txtに

CDA created on September 20th 1999 
Owner: Edward Jenner 
Access IDs, 
id = class\234ha, class\poi23, class\opiuj, cap\7y6t5 
dept = sub\6985de, ret\oiu87, class\234ha 
rec = ret\oiu87 

私はそれだけで一つのtime.i.e.のために印刷させることができますよクラスの\ 234ヘクタールのために私は期待どおりに

Removed from: E:\work\sample-access.txt 
Removed user : class\234ha 
Removed from row = id and dept 

私は、配列のオプションを使用してみましたする必要がありますが、私は結果が届かない理想的

Removed from: E:\work\sample-access.txt 
Removed user : class\234ha 
Removed from row = id 

として出力を得ていると私はしたくありません完全にコードやロジックを書き換えます。 Rubyのエキスパート、どんな提案も本当に役に立ちます。ありがとう。

答えて

0

は、おそらくあなたの質問に(最小限の変更で)最も簡単な解決策は、私が何かに

+0

ご提案をお寄せいただきありがとうございますけれどもを見落とすdidntの願ってい

# ... your code pool = [] # define array for current match text.each_line do |li| # ... your code pool << li.split(" ")[0] # add to array # ... your code log.puts "Removed from row :#{pool.join(' and ')}" # list all the ids joined by and # ... your code 

である。そのような値を追加する代わりに次のごとに単一の値を印刷行から削除サンプルaccess.txtに :行 'から削除行から削除サンプルaccess.txtに :ID から削除R:IDとDEP から削除ate-access.txt 行から削除されました:depとautomation' – dev

+0

申し訳ありませんがコメントを理解できません。各行(= id)には、現在のファイルに対して、それが削除されたすべてのセクションがループ内に保持されます。 'pool'は、ファイルごとにセクション(=行)を含みます。あなたの質問から答えが得られるはずです。 – insider

+0

お時間をいただきありがとうございます。私は別の方法で働いた。 – dev