Advice requested on array diff

D

Dennis Roberts

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
 
R

Robert Klemme

Dennis Roberts said:
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.inspect
end
end

"a" <-> "a"
"bbb" <-> "bbb"
"a" <-> "a"
"bbb" <-> "bbb"
"c" <-> "c"
"a" <-> "a"
"bbb" <-> "bbb"
"c" <-> "c"
"dd" <-> nil
=> 4...3

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top