Array read to seperate items

S

Stuart Clarke

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
 
R

Rob Biedenharn

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


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 Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
M

matt neuburg

Stuart Clarke said:
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

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.
 
M

matt neuburg

matt neuburg said:
Stuart Clarke said:
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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top