-1

Possible Duplicate:
ruby - Permutation between elements of an array

I'm coding a plugin in Google Sketchup with ruby and I faced a real problem while trying to permute different elements in arrays that are present in an another array, all this depending on a user combination.

I have an array of arrays like:

[["a, "b", c"], ["lol1", "lol2", lol3"], ["so1", "so2", "so3"]]

For a combination like:

[1, 2, 3]

The output should still same:

[["a", "b", "c"], ["lol1", "lol2", "lol3"], ["so1", "so2", "so3"]]

But for a combination like:

[2, 1, 3]

The output should be:

[["b", "a", "c"], ["lol2", "lol1", "lol3"], ["so2", "so1", "so3"]]

But for a combination like:

[3, 2, 1]

The output should be:

[["c", "b", a"], ["lol3", "lol2", "lol1"], ["so3", "so2", "so1"]]
9
  • 1
    What version of Ruby are you using? What code have you written? Commented Feb 5, 2013 at 22:48
  • 1
    @Mehdi Kamar, this array does not contain what you think it does. It is an Array of arrays, but each subarray contains just one string. Commented Feb 5, 2013 at 22:48
  • +! @steenslag, and, as such, this question is too localized to be answered. Commented Feb 5, 2013 at 22:52
  • @theTinMan Ruby 1.8.6 (within Google Sketchup 8). I have tried to delete the two useless strings of each array with thearray.map do |row| row.map do row.delete_at(1) end end two times so I keep only the first and then do a push of the two strings. But it didn't work Commented Feb 5, 2013 at 22:55
  • @steenslag How should I deal with that then ? Commented Feb 5, 2013 at 22:57

3 Answers 3

2
ar = [["a, b, c"], ["lol1, lol2, lol3"], ["so1, so2, so3"]]
p maybe_this = ar.map{|sub_ar| sub_ar.first.split(', ')}
#[["a", "b", "c"], ["lol1", "lol2", "lol3"], ["so1", "so2", "so3"]]
idx = [2,1,3]
#indexing of an array is zero-based
p idx.map!{|i| i-1} #[1,0,2]
p res = maybe_this.map{|sub_ar| sub_ar.values_at(*idx)}
#[["b", "a", "c"], ["lol2", "lol1", "lol3"], ["so2", "so1", "so3"]]

The short story: the values_at method takes multiple arguments; *idx splats an array into multiple arguments.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks you saved my life! I was wondering if I had an array containing a zero like [0,2,1] ?
Then don't do idx.map!{|i| i-1} . All the input you have to work with is (was) in a bit clumsy format. [0,2,1] is fine and needs no reworking.
1

Maybe you want this:

perm_idx = [1,2,3].permutation.to_a.index([2,3,1])
[["a", "b", "c"], ["lol1", "lol2", "lol3"], ["so1", "so2", "so3"]].map {|x| x.permutation.to_a[perm_idx] }

3 Comments

NoMethodError: undefined method `permutation' for [1, 2, 3]:Array
@MehdiKamar I guess you're using ruby 1.8? This question may help: stackoverflow.com/questions/1150359/…
Yes i'm using ruby 1.8.6. I'm going to check your proposition.
0

Not exactly sure what you want, but this might work for you:

a = [["a, b, c"], ["lol1, lol2, lol3"], ["so1, so2, so3"]]

def combi_with_join(ordering, arrays)
  ordering = ordering.map {|x| x - 1}
  arrays.map {|ar| [ar.first.split(/,\s*/).values_at(*ordering).join(", ")] }
end

puts combi_with_join([1, 2, 3], a).inspect
puts combi_with_join([2, 1, 3], a).inspect
puts combi_with_join([3, 2, 1], a).inspect

if you actually want an array of arrays...

def combi(ordering, arrays)
  ordering = ordering.map {|x| x - 1}
  arrays.map {|ar| ar.first.split(/,\s*/).values_at(*ordering) }
end

puts combi([1, 2, 3], a).inspect
puts combi([2, 1, 3], a).inspect
puts combi([3, 2, 1], a).inspect

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.