simple question: compare words

A

Ahmet Kilic

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???
 
H

Harry Kakueki

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???
--

Maybe this will do what you want.

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

p a&b


But, to kind of answer your question,


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
 
R

Rimantas Liubertas

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

$ye
$bu
a =3D "ali", "bel", "del","en"
b =3D "ali","bas","del","sx","en","sz","zen"

for xa in a
=C2=A0$ye=3D xa
end
b.each do |bx|
=C2=A0$bu =3D bx
=C2=A0 =C2=A0if $ye =3D=3D $bu
=C2=A0 =C2=A0 =C2=A0puts "#{$ye} and #{$bu}"
=C2=A0 =C2=A0end
end

result:
=3D> en and en

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=3D 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 &:=3D> ["ali", "bel", "del", "en"]
=3D> ["ali", "bas", "del", "sx", "en", "sz", "zen"]
=3D> ["ali", "del", "en"]

Regards,
Rimantas
 
R

Roy Bleasdale

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
 
A

Ahmet Kilic

7stud said:
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
 
H

Harry Kakueki

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
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top