[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Need help for finding difference between files

Clement Ow

5/14/2008 6:32:00 AM

Cheyne Li wrote:
> Hi, there
>
> Is there a simple way to find the difference between two files? I'm a
> new ruby learner. I'm not familiar with the Stander class in Ruby. Is
> there any can find the difference between two files and return a string
> or array?

What kinda difference do you wanna look at? is is file attributes?

You can try something like File.compare or File.fnmatch which wll return
true if the file matches..

If it's not something what you really need, please do explain your
situation abit more and maybe more people might be able to help ;)
--
Posted via http://www.ruby-....

7 Answers

Cheyne Li

5/14/2008 4:29:00 PM

0

Clement Ow wrote:
> Cheyne Li wrote:
>> Hi, there
>>
>> Is there a simple way to find the difference between two files? I'm a
>> new ruby learner. I'm not familiar with the Stander class in Ruby. Is
>> there any can find the difference between two files and return a string
>> or array?
>
> What kinda difference do you wanna look at? is is file attributes?
>
> You can try something like File.compare or File.fnmatch which wll return
> true if the file matches..
>
> If it's not something what you really need, please do explain your
> situation abit more and maybe more people might be able to help ;)

Ok, I need to compare the contents in 2 files. Print out each difference
in both files. for example, if i have 2 files: f1.txt, f2.txt

in f1: in f2:
I'm learning ruby. I'm learning ruby.
I'm not good at it. I'm good at it.

So, I want to know if there a function can return an array that contains
"I'm not good at it." and "I'm good at it."

I wonder if I have to compare line by line or there is function in Ruby
would do that for me.
--
Posted via http://www.ruby-....

Michael Linfield

5/14/2008 4:54:00 PM

0

> I wonder if I have to compare line by line or there is function in Ruby
> would do that for me.

file1 = File.readlines("f1.txt")
file2 = File.readlines("f2.txt")

file1&file2
#=> "I'm learning ruby."

The '&' symbol can be used to compare arrays, it will return any matches
between the 2.

Regards,

- Mac

--
Posted via http://www.ruby-....

Todd Benson

5/14/2008 4:57:00 PM

0

On Wed, May 14, 2008 at 11:29 AM, Cheyne Li
<happy.go.lucky.clr@gmail.com> wrote:
> Clement Ow wrote:
>> Cheyne Li wrote:
>>> Hi, there
>>>
>>> Is there a simple way to find the difference between two files? I'm a
>>> new ruby learner. I'm not familiar with the Stander class in Ruby. Is
>>> there any can find the difference between two files and return a string
>>> or array?
>>
>> What kinda difference do you wanna look at? is is file attributes?
>>
>> You can try something like File.compare or File.fnmatch which wll return
>> true if the file matches..
>>
>> If it's not something what you really need, please do explain your
>> situation abit more and maybe more people might be able to help ;)
>
> Ok, I need to compare the contents in 2 files. Print out each difference
> in both files. for example, if i have 2 files: f1.txt, f2.txt
>
> in f1: in f2:
> I'm learning ruby. I'm learning ruby.
> I'm not good at it. I'm good at it.
>
> So, I want to know if there a function can return an array that contains
> "I'm not good at it." and "I'm good at it."
>
> I wonder if I have to compare line by line or there is function in Ruby
> would do that for me.

If you don't mind reading the whole files in to memory, you can try
doing something like...

arr1 = File.open("f1", "r").readlines
arr2 = File.open("f2", "r").readlines
arr1 - arr2 # all in arr1 not in arr2
arr2 - arr1 # all in arr2 not in arr1
arr1 || arr2 # all that both share (intersection)
arr1 && arr2 # every line from both (union)

hth,
Todd

Todd Benson

5/14/2008 5:01:00 PM

0

On Wed, May 14, 2008 at 11:57 AM, Todd Benson <caduceass@gmail.com> wrote:
> On Wed, May 14, 2008 at 11:29 AM, Cheyne Li
> <happy.go.lucky.clr@gmail.com> wrote:
>> Clement Ow wrote:
>>> Cheyne Li wrote:
>>>> Hi, there
>>>>
>>>> Is there a simple way to find the difference between two files? I'm a
>>>> new ruby learner. I'm not familiar with the Stander class in Ruby. Is
>>>> there any can find the difference between two files and return a string
>>>> or array?
>>>
>>> What kinda difference do you wanna look at? is is file attributes?
>>>
>>> You can try something like File.compare or File.fnmatch which wll return
>>> true if the file matches..
>>>
>>> If it's not something what you really need, please do explain your
>>> situation abit more and maybe more people might be able to help ;)
>>
>> Ok, I need to compare the contents in 2 files. Print out each difference
>> in both files. for example, if i have 2 files: f1.txt, f2.txt
>>
>> in f1: in f2:
>> I'm learning ruby. I'm learning ruby.
>> I'm not good at it. I'm good at it.
>>
>> So, I want to know if there a function can return an array that contains
>> "I'm not good at it." and "I'm good at it."
>>
>> I wonder if I have to compare line by line or there is function in Ruby
>> would do that for me.
>
> If you don't mind reading the whole files in to memory, you can try
> doing something like...
>
> arr1 = File.open("f1", "r").readlines
> arr2 = File.open("f2", "r").readlines
> arr1 - arr2 # all in arr1 not in arr2
> arr2 - arr1 # all in arr2 not in arr1
> arr1 || arr2 # all that both share (intersection)
> arr1 && arr2 # every line from both (union)

Sorry that's | and &, _not_ || and && and I had it backwards...

arr1 | arr2 # all of both
arr1 & arr2 # all that intersect

sorry for confusion,
Todd

Cheyne Li

5/14/2008 6:24:00 PM

0

Michael Linfield wrote:
>> I wonder if I have to compare line by line or there is function in Ruby
>> would do that for me.
>
> file1 = File.readlines("f1.txt")
> file2 = File.readlines("f2.txt")
>
> file1&file2
> #=> "I'm learning ruby."
>
> The '&' symbol can be used to compare arrays, it will return any matches
> between the 2.
>
> Regards,
>
> - Mac

Thanks man. I found that using "-" can find all differences

diff = file1 - file2
--
Posted via http://www.ruby-....

Reid Thompson

5/15/2008 2:47:00 AM

0

Cheyne Li wrote:
> Clement Ow wrote:
>> Cheyne Li wrote:
>>> Hi, there
>>>
>>> Is there a simple way to find the difference between two files?

http://en.wikipedia.org...

Dominic Sisneros

5/15/2008 5:18:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

ruby diff-lcs will do this

On Wed, May 14, 2008 at 12:32 AM, Clement Ow <clement.ow@asia.bnpparibas.com>
wrote:

> Cheyne Li wrote:
> > Hi, there
> >
> > Is there a simple way to find the difference between two files? I'm a
> > new ruby learner. I'm not familiar with the Stander class in Ruby. Is
> > there any can find the difference between two files and return a string
> > or array?
>
> What kinda difference do you wanna look at? is is file attributes?
>
> You can try something like File.compare or File.fnmatch which wll return
> true if the file matches..
>
> If it's not something what you really need, please do explain your
> situation abit more and maybe more people might be able to help ;)
> --
> Posted via http://www.ruby-....
>
>