[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Advice requested on array diff

Dennis Roberts

12/19/2004 8:52:00 AM

Hi I am new to Ruby and I am working on my first program.

I have two arrays, I want to see the difference between them. I was
thinking of ary1 - ary2 but sometimes it should be ary2 - ary1 and I am
not sure at run time which one will be different.

So then I thought okay I will check if they are the same and if they are
not then I will print both of them out side by side in report fashion.

So in a method I call aryDiffReport I set two variables: count which is
the length of the bigger array, and len which is the length of the
largest item in either array (for this I wrote my own method
*_longestItem - couldn't find anything built in to do it).

Using the two variables and sprintf I construct a third array that looks
something like the following when printed out:

one <-> one
two <-> two
a <-> b
<-> 12

Okay so my first thoughts is this will be real slow with big arrays.

Being new to Ruby I was thinking I could get some advice on what I am
trying to do. Is there a better way? I appreciate all feedback good or
bad.

* I kinda thought this was a neat little method so I decided to share:)
def _longestItem(ary1, ary2)
itemlen = 0
ary1.each do |i|
if i.length > itemlen
then
itemlen = i.length
end
end
ary2.each do |i|
if i.length > itemlen
then
itemlen = i.length
end
end
return itemlen
end


1 Answer

Robert Klemme

12/19/2004 12:23:00 PM

0


"Dennis Roberts" <dennisr@spacerodent.org> schrieb im Newsbeitrag
news:41C54111.5040701@spacerodent.org...
> Hi I am new to Ruby and I am working on my first program.
>
> I have two arrays, I want to see the difference between them. I was
> thinking of ary1 - ary2 but sometimes it should be ary2 - ary1 and I am
> not sure at run time which one will be different.
>
> So then I thought okay I will check if they are the same and if they are
> not then I will print both of them out side by side in report fashion.
>
> So in a method I call aryDiffReport I set two variables: count which is
> the length of the bigger array, and len which is the length of the largest
> item in either array (for this I wrote my own method *_longestItem -
> couldn't find anything built in to do it).
>
> Using the two variables and sprintf I construct a third array that looks
> something like the following when printed out:
>
> one <-> one
> two <-> two
> a <-> b
> <-> 12
>
> Okay so my first thoughts is this will be real slow with big arrays.
>
> Being new to Ruby I was thinking I could get some advice on what I am
> trying to do. Is there a better way? I appreciate all feedback good or
> bad.
>
> * I kinda thought this was a neat little method so I decided to share:)
> def _longestItem(ary1, ary2)
> itemlen = 0
> ary1.each do |i|
> if i.length > itemlen
> then
> itemlen = i.length
> end
> end
> ary2.each do |i|
> if i.length > itemlen
> then
> itemlen = i.length
> end
> end
> return itemlen
> end

It's more flexible to calculate maxlen for a single array only. If you want
to deal with large arrays gracefully, then this is probably what I'd do:

def max_element_len(enum)
enum.inject(0) {|max,x|l = x.inspect.length; l > max ? l : max}
end

def array_diff(a1, a2)
l1, l2 = max_element_len( a1 ), max_element_len( a2 )
pattern = "%-#{l1}s <-> %-#{l2}s\n"

a1.zip(a2) {|a,b| printf pattern, a.inspect, b.inspect }

for i in a1.length ... a2.length
printf pattern, "nil", a2[i].inspect
end
end


>> array_diff %w{a bbb c}, %w{a bbb c}
"a" <-> "a"
"bbb" <-> "bbb"
"c" <-> "c"
=> 3...3
>> array_diff %w{a bbb c}, %w{a bbb c dd}
"a" <-> "a"
"bbb" <-> "bbb"
"c" <-> "c"
nil <-> "dd"
=> 3...4
>> array_diff %w{a bbb c dd}, %w{a bbb c}
"a" <-> "a"
"bbb" <-> "bbb"
"c" <-> "c"
"dd" <-> nil
=> 4...3

Kind regards

robert