ruby - Permutation between elements of an array -
ruby - Permutation between elements of an array -
i'm coding plugin in google sketchup ruby , faced real problem while trying permute 2 arrays nowadays in array depending on user combination.
i have array of arrays [["1"],["lol"], ["so"]]
when have combination <[1, 2, 3]
it's fine, should remain same : [["1"],["lol"], ["so"]]
but when have combination [2, 3, 1]
, output should : [["lol"], ["so"], ["1"]]
for [3,1,2]
=> [["so"], ["1"], ["lol"]]
...etc
edit sorry guys forgot array have bit : [["1, 2, 3"], ["lol1, lol2, lol3"], ["so1, so2, so3"]]
combination [2, 3, 1]
output should : [["2, 3, 1"], ["lol2, lol3, lol1"], ["so2, so3, so1"]]
thanks helping me out.
you utilize collect:
array = [["1"],["lol"], ["so"]] indexes = [2, 1, 3] indexes.collect {|i| array[i-1]} #=> [["lol"], ["1"], ["so"]]
if set indexes 0-based drop -1
split , map can used turn strings values:
"1, 2, 3".split(",").map { |i| i.to_i} # [1, 2, 3]
you can split strings
"lol2, lol3, lol1".split(/, /) #=> ["lol2", "lol3", "lol1"]
you should able set above want.
ruby-on-rails ruby
Comments
Post a Comment