# Simple question: compare words

I want to compare same words in a and b then,
I wrote this,

\$ye
\$bu
a = “ali”, “bel”, “del”,“en”
b = “ali”,“bas”,“del”,“sx”,“en”,“sz”,“zen”

for xa in a
\$ye= xa
end
b.each do |bx|
\$bu = bx
if \$ye == \$bu
puts “#{\$ye} and #{\$bu}”
end
end

result:
=> en and en

but result is only “en and en”. why the last one?
where is the others ali and del???

Ahmet K. wrote:

for xa in a
\$ye= xa
end

That is the equivalent to:

\$ye = “ali”
\$ye = “bel”
\$ye = “del”
\$ye = “en”

which gives you:

puts \$ye

–output:–
“en”

On Fri, Sep 18, 2009 at 2:14 PM, Ahmet K. [email protected]
wrote:

## end but result is only “en and en”. why the last one? where is the others ali and del???

Maybe this will do what you want.

a = [“ali”, “bel”, “del”,“en”]
b = [“ali”,“bas”,“del”,“sx”,“en”,“sz”,“zen”]

p a&b

a = [“ali”, “bel”, “del”,“en”]
b = [“ali”,“bas”,“del”,“sx”,“en”,“sz”,“zen”]

for xa in a
\$ye= xa
end

puts \$ye #Look here.

p \$ye
b.each do |bx|
\$bu = bx

puts \$ye #And look here.

if \$ye == \$bu
puts “#{\$ye} and #{\$bu}”
end
end

Harry

Try adding puts \$ye after for loop to see what value it contains.

for xa in a
\$ye= xa
end
puts \$ye

This should give you a clue to your problem.

Then look at the documentation on Ruby arrays class - Ruby provides lots
of lovely ways of processing arrays.

for example try
array & other_array

end
but result is only “en and en”. why the last one?
where is the others ali and del???

You get only en, because by the end of this loop:

for xa in a
\$ye= xa
end

you have the last item (“en”) in a assigned to \$ye and then continue
to compare this last value to the b.
No wonder it finds only “en”.

In Ruby you can find common element in array with &:

a = %w{ali bel del en}
=> [“ali”, “bel”, “del”, “en”]

b = %w{ali bas del sx en sz zen}
=> [“ali”, “bas”, “del”, “sx”, “en”, “sz”, “zen”]

a & b
=> [“ali”, “del”, “en”]

Regards,
Rimantas

On Fri, Sep 18, 2009 at 4:08 PM, Ahmet K. [email protected]
wrote:

\$ye = “del”
\$ye = “en”

which gives you:

puts \$ye

–output:–
“en”

Yes, I want that.

No, you don’t. You go through all of them but you don’t do anything with them.
Then you compare only the last one to the elements in the other array.

Harry

7stud – wrote:

Ahmet K. wrote:

for xa in a
\$ye= xa
end

That is the equivalent to:

\$ye = “ali”
\$ye = “bel”
\$ye = “del”
\$ye = “en”

which gives you:

puts \$ye

–output:–
“en”

Yes, I want that. I want to compare them by one by with other words.
Also, It is not Array it is String.
because if I use \$ye.class it gives me String
and I got this error. undefined method `&’ for “en”:String