Wednesday, 15 May 2013

arrays - Collect all not in using Ruby -


I have been truncated this small code.

I do not know Ruby and I think it's a great opportunity to implement it.

I want to print all the rows in the file e , which are not in the file c . Each row is a number.

This is what I have:

  e = file. New ('E') Readlines c = File.new ('C'). Readline x = E. Collect. Items | C.include? (item)? The problem is that both files can have spaces, and for this reason the same number can not be treated like this. (Like "1234" is different from "1234")  

How do I need my code to fix this? I have tried c.include? (Item.strip) .. but it does not seem to work.

You are tab when you readlines will help, i.e.

  e = file. Roline ('e'). Map {| X | X.strip} c = File.readlines ('c'). Map {| X | X.strip}  

If you do not want an empty string, you can use archive , or perhaps select :

x = e.select {| I | ! C.include? (I)}

edit : Or, as suggested,

  x = ec  

No comments:

Post a Comment