# Array read to seperate items

**URL:** <https://www.ruby-forum.com/t/array-read-to-seperate-items/164376>\
**Category:** Ruby\
**Created:** [March 31, 2009, 5:27pm UTC](https://www.ruby-forum.com/t/array-read-to-seperate-items/164376 "2009-03-31T17:27:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [March 31, 2009, 5:27pm UTC](https://www.ruby-forum.com/t/array-read-to-seperate-items/164376/1 "2009-03-31T17:27:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [March 31, 2009, 5:56pm UTC](https://www.ruby-forum.com/t/array-read-to-seperate-items/164376/2 "2009-03-31T17:56:43Z")

</div>

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/](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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [March 31, 2009, 6:16pm UTC](https://www.ruby-forum.com/t/array-read-to-seperate-items/164376/3 "2009-03-31T18:16:04Z")

</div>

matt neuburg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [March 31, 2009, 6:11pm UTC](https://www.ruby-forum.com/t/array-read-to-seperate-items/164376/4 "2009-03-31T18:11:23Z")

</div>

Stuart C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
