Array updation in ruby -


given 2 arrays:

s1 = ["arun#2#very shop","mani#4#i glad meet sir.","anu#2#not bad"] s2 = ["first","second","third"] 

i want new array s3 like:

s3 = ["arun#2#very shop#first","mani#4#i glad meet sir#second.","anu#2#not bad#third"] 

try this:

> s3 = [s1,s2].transpose.map{|a| a.join("#")}  => ["arun#2#very shop#first", "mani#4#i glad meet sir.#second", "anu#2#not bad#third"] 

note: work if 2 array has same number of elements.

alternative option : work number of elements in 2 array

> s1.zip(["#"].cycle, s2).map(&:join)  => ["arun#2#very shop#first", "mani#4#i glad meet sir.#second", "anu#2#not bad#third"] 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -