Array read to seperate items

Hi all,

This might be a stupid question.

I have an array which gets data added to in a loop like so:

a b c d e f
a b c d e f

I am aware calling join("\n")on this array will separate each line of
the array with a new line, but I want to separate the individual items
and load them into there own arrays for example

array a = a, a
array b = b, b

etc etc.

In summary I want to output my data exactly how it is, but because I
want to load into structured columns on my GUI I want the data broken
down so it can be loaded to the relevant columns.

Thanks in advance

On Mar 31, 2009, at 11:27 AM, Stuart C. wrote:

the array with a new line, but I want to separate the individual items

Thanks in advance

Posted via http://www.ruby-forum.com/.

Array#transpose

irb> [[1,2,3,4,5],%w[a b c d e]].transpose
=> [[1, “a”], [2, “b”], [3, “c”], [4, “d”], [5, “e”]]

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

matt neuburg [email protected] wrote:

array a = a, a
array b = b, b

Are you talking about something like this?

arr = [“hey ho hey nonnie no”, “bee bop a ding dong”, “a b c d e”]
arr = arr.map {|s| s.split(" ")}
arr = arr.shift.zip(*arr)

Last line cute but unnecessary, forgot about “transpose” (see msg from
Rob B.):

arr = [“hey ho hey nonnie no”, “bee bop a ding dong”, “a b c d e”]
arr = arr.map {|s| s.split(" ")}.transpose
p arr
#=> [[“hey”, “bee”, “a”], [“ho”, “bop”, “b”], [“hey”, “a”, “c”],
[“nonnie”, “ding”, “d”], [“no”, “dong”, “e”]]

m.

Stuart C. [email protected] wrote:

array b = b, b
Are you talking about something like this?

arr = [“hey ho hey nonnie no”, “bee bop a ding dong”, “a b c d e”]
arr = arr.map {|s| s.split(" ")}
arr = arr.shift.zip(*arr)
p arr
#=> [[“hey”, “bee”, “a”], [“ho”, “bop”, “b”], [“hey”, “a”, “c”],
[“nonnie”, “ding”, “d”], [“no”, “dong”, “e”]]

m.