Array Comparison

A

aidy

Hi Guys

I have compared an array and '0' is being returned which indicates to
me that that array is equal

p t = $ie.table:)index, '2').to_a.flatten
p t <=> ["HINCKLEY", "HINDHEAD", "HINTON ST GEORGE"]

Now would it be possible for me to extract any differences from this
array (that is, when the array is not equal)?

Or should I really be using Test::Unit: to assert array equality?

aidy
 
R

Robert Klemme

Hi Guys

I have compared an array and '0' is being returned which indicates to
me that that array is equal

p t = $ie.table:)index, '2').to_a.flatten
p t <=> ["HINCKLEY", "HINDHEAD", "HINTON ST GEORGE"]

Now would it be possible for me to extract any differences from this
array (that is, when the array is not equal)?

Or should I really be using Test::Unit: to assert array equality?

You can use Array#zip (block form) to determine any differences.

["HINCKLEY", "HINDHEAD", "HINTON ST GEORGE"].zip(t) do |a,b|
if a != b
print "Difference: ", a.inspect, " ", b.inspect, "\n"
end
end

Kind regards

robert
 
P

Pit Capitain

Robert said:
You can use Array#zip (block form) to determine any differences.

["HINCKLEY", "HINDHEAD", "HINTON ST GEORGE"].zip(t) do |a,b|
if a != b
print "Difference: ", a.inspect, " ", b.inspect, "\n"
end
end

But note that this doesn't work if the second array contains more
elements than the first:

[1].zip [1, 2, 3] # => [[1, 1]]

It is also problematic if the arrays can contain nil:

[nil, nil, nil].zip [nil] # => [[nil, nil], [nil, nil], [nil, nil]]

Regards,
Pit
 
K

Kroeger, Simon (ext)

=20
From: aidy [mailto:[email protected]]=20
Sent: Thursday, August 17, 2006 1:35 PM
=20
Hi Guys
=20
I have compared an array and '0' is being returned which indicates to
me that that array is equal
=20
p t =3D $ie.table:)index, '2').to_a.flatten
p t <=3D> ["HINCKLEY", "HINDHEAD", "HINTON ST GEORGE"]
=20
Now would it be possible for me to extract any differences from this
array (that is, when the array is not equal)?
=20
Or should I really be using Test::Unit: to assert array equality?
=20
aidy

t =3D [2, 3, 4, 5]

# use =3D=3D if you want to test for equality
p t =3D=3D [2, 3, 4, 5] # =3D> true

if t !=3D [3, 4, 5, 6]
# you may use the array set methods to show the differences
p t - [3, 4, 5, 6] #=3D> [2]
p [3, 4, 5, 6] - t #=3D> [6]
p ([3, 4, 5, 6] | t) - ([3, 4, 5, 6] & t) #=3D> [6, 2]
end

cheers

Simon
 
A

aidy

Hi All,

Thanks for the feedback, but I need to be bothered about order, so I
have decided to use an iterator

t = $ie.table:)index, '2').to_a.flatten
t.each {|a|
p "table #{a}"
p "line #{line}"
if /line/ =~ a; p 'match' else; p 'not match'; end
}

however I can not match the two objects, when the ouput shows
"table HINCKLEY"
"line HINCKLEY "
"not match"
"table HINTON ST GEORGE"
"line HINTON ST GEORGE"

cheers

aidy
ps I thought it was initially the trailing whitespace; that is why I
have RegEx'ed the var 'line'.
 
A

aidy

Jeffrey Schwab wrote
Could you please post a complete example, including a table and some
input text?

I have an HTML table

for example

HINCKLEY
HINDHEAD
HINTON ST GEORGE

If have a file the I read which inputs data and compares the above
result with the expected result

*********************************************** TESTID_80
Town:
HIN
Country2:
GB
Search-Results2:
3
Expected-Result:
HINCKLEY
HINDHEAD
HINTON ST GEORGE
END:

this table, I can read as a two dimesional array and flatten

here is most of the code

when /^(Provinces|Town):$/
task = :provinces
when /^Search-Results:$/
task = :search
when /^Search-Results2:$/
task = :search2
when /^Country2:$/
task = :country2
when /^Expected-Result:$/
task = :verify
when /^END:$/
task = :end
$ie.close
else
case task
when :country
$ie.text_field:)name, 'C1').set(line)
when :search
$ie.text_field:)name, 'NR1').set(line)
$ie.button:)value, 'Search').click
when :provinces
$ie.text_field:)name, 'T1').set(line)
when :search2
$ie.text_field:)name, 'NR2').set(line)
$ie.button:)value, 'Get Towns').click
when :country2
$ie.text_field:)name, 'C2').set(line)
if @filename == "provinces.txt"
$ie.button:)value, 'Get Provinces').click
end
when :verify
t = $ie.table:)index, '2').to_a.flatten
t.each {|a|
p "table #{a}"
p "line #{line}"
if /line/ =~ a; p 'match' else; p 'not match'; end
}

The problem I have having is comparing the flattened array with what I
have in the file as an expected result

Cheers

aidy
 
C

ChrisH

aidy said:
t = $ie.table:)index, '2').to_a.flatten
t.each {|a|
p "table #{a}"
p "line #{line}"
if /line/ =~ a; p 'match' else; p 'not match'; end
}

try
/#{line}/ =~ a

cheers
 
A

aidy

ChrisH said:
try
/#{line}/ =~ a

cheers

I need shooting. However I get a match with this

"table HINTON ST GEORGE"
"line HINTON ST GEORGE"
match

but not with a trailing space
"table HINCKLEY"
"line HINCKLEY "
not match

I can't see a trim in Ruby.

I tried this also /#{line}s*/

Thanks so much

aidy
 

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

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,089
Members
47,689
Latest member
kilaocrhtbfnr

Latest Threads

Top