Forum: Ruby Fwd: In search of elegant indices searching

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Tim B. (Guest)
on 2006-05-16 15:07
(Received via mailing list)
Your result is confusing me:

Not really sure what you're trying to accomplish, if you want to
determine the indices at which two arrays contain identical values,
you could try:

def comp arr1, arr2
  arr1.each_with_index {|val, i|
    res.push val==arr2[i]
comp [1,1,3,1,5,6], [1,2,3,1,4,5] # -> [true, false, true, true, false,

if you'd like to determine which values occur in both arrays, try the
intersection operator `&`:

[1,1,3,1,5,6] & [1,2,3,1,4,5] # -> [1, 3, 5]

This topic is locked and can not be replied to.