How do I find .index of a multidimensional array

后端 未结 7 1661
-上瘾入骨i
-上瘾入骨i 2021-02-08 19:34

Tried web resources and didnt have any luck and my visual quick start guide.

If I have my 2d/multidimensional array:

 array = [[\'x\', \'x\',\' x\',\'x\'         


        
7条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-08 20:18

    This will do it:

    array = [['x', 'x',' x','x'],
             ['x', 'S',' ','x'],
             ['x', 'x',' x','x']]
    
    p array.index(array.detect{|aa| aa.include?('S')}) # prints 1
    

    If you also want 'S's index in the sub array you could:

    row = array.detect{|aa| aa.include?('S')}
    p [row.index('S'), array.index(row)] # prints [1,1]
    

提交回复
热议问题