Reputation: 63
Given two arrays:
s1 = ["Arun#2#very good shop","Mani#4#i am very glad to meet you sir.","Anu#2#not bad"]
s2 = ["first","second","third"]
I want a new array s3
like:
s3 = ["Arun#2#very good shop#first","Mani#4#i am very glad to meet you sir#second.","Anu#2#not bad#third"]
Upvotes: 2
Views: 71
Reputation: 10251
Try this:
> s3 = [s1,s2].transpose.map{|a| a.join("#")}
=> ["Arun#2#very good shop#first", "Mani#4#i am very glad to meet you sir.#second", "Anu#2#not bad#third"]
Note: This will work if two array has same number of elements.
Alternative Option : this will work for any number of elements in two array
> s1.zip(["#"].cycle, s2).map(&:join)
=> ["Arun#2#very good shop#first", "Mani#4#i am very glad to meet you sir.#second", "Anu#2#not bad#third"]
Upvotes: 2
Reputation: 8821
s3 = s1.zip(s2).map{ |x| x.join('#')}
=> ["Arun#2#very good shop#first", "Mani#4#i am very glad to meet you sir.#second", "Anu#2#not bad#third"]
Upvotes: 8
Reputation: 16506
Assuming you want to append elements of s2
to s1
at corresponding index after #
:
s3 = []
s1.each_with_index{|a, i| s3 << a + "#" + s2[i].to_s}
s3
# => ["Arun#2#very good shop#first", "Mani#4#i am very glad to meet you sir.#second", "Anu#2#not bad#third"]
Upvotes: 0