Forum: Ruby Fwd: In search of elegant indices searching

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
62002cee15efcf4628cd7efc19425a07?d=identicon&s=25 Tim Becker (Guest)
on 2006-05-16 13:07
(Received via mailing list)
Your result is confusing me:
>{{'x'=>1,'w'=>1}=>[0,3],{'x'=>3,'w'=>3}=>[2],{'x'=>5,'w'=>4}=>[4],{'x'=>6,
 'w'=>5}=>[5]}.

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
  res=[]
  arr1.each_with_index {|val, i|
    res.push val==arr2[i]
  }
end
comp [1,1,3,1,5,6], [1,2,3,1,4,5] # -> [true, false, true, true, false,
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]

   -tim
This topic is locked and can not be replied to.