Finding indexes of each element in a multidimensional array in ruby

Posted by Shreyas Satish on Stack Overflow See other posts from Stack Overflow or by Shreyas Satish
Published on 2010-04-07T15:35:39Z Indexed on 2010/04/07 16:23 UTC
Read the original article Hit count: 342

Filed under:
|
|
|

Eg

:a=[["hello", "world"], ["good", "lord"], ["hello", "lord"]]

I need to find and record the indexes of each word with respect to the super-array. i.e

hello => 0,2
world => 0
lord => 1,2.

here's my shot ,but its very amateurish and lengthy.

all_tokens=tokens.flatten
all_tokens.each do|keyword|                                                                                      
    tokens.each do|token_array|
        if token_array.include?keyword
            x << i
        end
        i=i+1
    end    
    y[k] = x.clone
    y=y.clear
end 

© Stack Overflow or respective owner

Related posts about ruby

Related posts about multidimensional