Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Ruby on Rails (http://www.go4expert.com/forums/ruby-on-rails/)
-   -   Ruby - 2D array column to 1D array (http://www.go4expert.com/forums/ruby-2d-array-column-1d-array-t29727/)

Rofellos 11Jul2013 01:36

Ruby - 2D array column to 1D array
 
Hello,

Is there any method to do this without using a loop to collect the values?


Ex.:
2D array: array =
[1] [11] [21] [31] [41]
[2] [12] [22] [32] [42]
[3] [13] [23] [33] [43]
[4] [14] [24] [34] [44]
[5] [15] [25] [35] [45]
[6] [16] [26] [36] [46]
[7] [17] [27] [37] [47]
[8] [18] [28] [38] [48]
[9] [19] [29] [39] [49]
[10] [20] [30] [40] [50]

To get the second row, it can be done like this array[1]
return 1D array: [2] [12] [22] [32] [42]

Is there something like this to get columns?

Rofellos 12Jul2013 19:54

Re: Ruby - 2D array column to 1D array
 
Solved:

To get the first row:
a[0]

To get the first column:
a.transpose[0]


All times are GMT +5.5. The time now is 21:53.