[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Array Comparison

aidy

8/17/2006 11:33:00 AM

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

15 Answers

Robert Klemme

8/17/2006 11:52:00 AM

0

On 17.08.2006 13:32, aidy wrote:
> 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

Pit Capitain

8/17/2006 12:48:00 PM

0

Robert Klemme schrieb:
> 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

Kroeger, Simon (ext)

8/17/2006 1:04:00 PM

0


> From: aidy [mailto:aidy.rutter@gmail.com]
> Sent: Thursday, August 17, 2006 1:35 PM
>
> 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

t = [2, 3, 4, 5]

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

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

cheers

Simon

Jeff Schwab

8/17/2006 1:19:00 PM

0

aidy wrote:
> 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?

ftp://ftp.se.freebsd.org/pub/FreeBSD/ports/local-distfiles/matusita/ruby-diff/diff-...
http://www.freshports.org/textproc/...

If you don't care about differences in the order of the elements, just
use Sets:

a1 = ['animal', 'mineral', 'vegetable']
a2 = ['vegetable', 'mineral']

require 'set'

s = Set.new
s |= a1
s -= a2
puts "elements in a1 but not a2:\n"
s.each {|e| puts "\t#{e}" }

s = Set.new
s |= a2
s -= a1
puts "elements in a2 but not a1:\n"
s.each {|e| puts "\t#{e}" }

aidy

8/17/2006 2:20:00 PM

0

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'.

Jeff Schwab

8/17/2006 2:40:00 PM

0

aidy wrote:
> 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'.

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

aidy

8/17/2006 3:13:00 PM

0

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

Chris Hulan

8/17/2006 3:14:00 PM

0


aidy wrote:
> 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

Jeff Schwab

8/17/2006 3:27:00 PM

0

aidy wrote:
> 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
....
> this table, I can read as a two dimesional array and flatten
>
> here is most of the code
....
> The problem I have having is comparing the flattened array with what I
> have in the file as an expected result

Thanks, although I think aidy's reply just fixed your problem. :)

aidy

8/17/2006 3:43:00 PM

0


ChrisH wrote:
> 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