Array Comparison

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

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

···

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?

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

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

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

You could always add an XOR function to the Array class.

class Array
  def XOR(i)
    return (i | self) - (i & self)
  end

  def ^(i)
    return XOR(i)
  end
end

It should be there already anyway, even if it is just syntactic sugar...

···

On 8/17/06, Kroeger, Simon (ext) <simon.kroeger.ext@siemens.com> wrote:

> 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

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

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

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

aidy wrote:

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

trim would be strip:

http://ruby-doc.org/core/classes/String.html#M000625

/#{line}s*/ won't work, because that's saying:

Is "HINCKLEY " in "HINCKLEY" ?

Which of course it isn't.

Also, as a side note, it might be simpler to use the ternary operator:

if /#{line}/ =~ a; p 'match' else; p 'not match'; end

becomes

/#{line}/ =~ a ? p 'match' : p 'not match'

-Justin

aidy wrote:

I can't see a trim in Ruby.

Look at chomp and chop

ChrisH wrote:

aidy wrote:
>
> I can't see a trim in Ruby.

Look at chomp and chop

The Ruby Extensions Project has String#trim:
http://extensions.rubyforge.org/rdoc/

Thomas, Mark - BLS CTR wrote:

ChrisH wrote:
> aidy wrote:
> >
> > I can't see a trim in Ruby.
>
> Look at chomp and chop

The Ruby Extensions Project has String#trim:
http://extensions.rubyforge.org/rdoc/

Superfluous. Use .rstrip.

Thomas, Mark - BLS CTR wrote:

ChrisH wrote:

The Ruby Extensions Project has String#trim:
http://extensions.rubyforge.org/rdoc/

Thanks for the link. I think these extensions look quite useful and
intuitive and maybe should be a part of the one click-installer. 'trim'
for example is a keyword in VB.

Thanks for all the help

Aidy