Forum: Ruby Array read to seperate items

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cf25fbf53c67e27d95845e77e949b56f?d=identicon&s=25 Stuart Clarke (sclarke)
on 2009-03-31 17:27
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
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2009-03-31 17:56
(Received via mailing list)
On Mar 31, 2009, at 11:27 AM, Stuart Clarke 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 Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
Fbb4d027695dfdf76bf448b15d7e306a?d=identicon&s=25 matt neuburg (Guest)
on 2009-03-31 18:11
(Received via mailing list)
Stuart Clarke <stuart.clarke1986@gmail.com> 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.
Fbb4d027695dfdf76bf448b15d7e306a?d=identicon&s=25 matt neuburg (Guest)
on 2009-03-31 18:16
(Received via mailing list)
matt neuburg <matt@tidbits.com> 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.
This topic is locked and can not be replied to.