# Extracting and inserting a "column" from/into an array

**URL:** <https://www.ruby-forum.com/t/extracting-and-inserting-a-column-from-into-an-array/170365>\
**Category:** Ruby\
**Created:** [June 16, 2009, 5:34pm UTC](https://www.ruby-forum.com/t/extracting-and-inserting-a-column-from-into-an-array/170365 "2009-06-16T17:34:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chauk-mean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d07c76/32.png) [@chauk-mean](https://www.ruby-forum.com/u/chauk-mean)\
**Post date:** [June 16, 2009, 5:34pm UTC](https://www.ruby-forum.com/t/extracting-and-inserting-a-column-from-into-an-array/170365/1 "2009-06-16T17:34:34Z")

</div>

Hi,

For image processing, I have to do the following operations :

1/ I have an array and I want to extract every 4 element into another  
array :

# r=red, g=green, b=blue, a=alpha

img\_data = [“r1”, “g1”, “b1”, “a1”, "r2, “g2”, “b2”, “a2”]

=\> [“a1”, “a2”] # the array of every four elements i.e. alpha data

It’s like extracting the fourth column of a matrix of n rows and 4  
columns.

2/ Conversely, I have an array and I need to add elements at every 3  
elements

img\_data = [“r1”, “g1”, “b1”, "r2, “g2”, “b2”]  
alpha\_data = [“a1”, “a2”] # the elements to add

=\> [“r1”, “g1”, “b1”, “a1”, "r2, “g2”, “b2”, “a2”]

It’s like inserting a column to a matrix of n rows and 3 columns.

I’m wondering if there is an easy or seamless way to do these  
operations.  
Thanks in advance for any tips.

Cheers.

Chauk-Mean.

---

<div class="post-metadata">

**Author:** ![chauk-mean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d07c76/32.png) [@chauk-mean](https://www.ruby-forum.com/u/chauk-mean)\
**Post date:** [June 16, 2009, 7:57pm UTC](https://www.ruby-forum.com/t/extracting-and-inserting-a-column-from-into-an-array/170365/2 "2009-06-16T19:57:17Z")

</div>

Array is an Enumerable so when you require ‘enumerator’ you get a bunch  
of cool ways of slicing it up.

> 1/ I have an array and I want to extract every 4 element into another  
> array :
> 
> # r=red, g=green, b=blue, a=alpha
> 
> img\_data = [“r1”, “g1”, “b1”, “a1”, "r2, “g2”, “b2”, “a2”]
> 
> =\> [“a1”, “a2”] # the array of every four elements i.e. alpha data

result = img\_data.enum\_slice(4).map {|arr| arr[3]}

> =\> [“r1”, “g1”, “b1”, “a1”, "r2, “g2”, “b2”, “a2”]  
> result = img\_data.enum\_slice(3).zip(alpha\_data).flatten

m.

---

<div class="post-metadata">

**Author:** ![chauk-mean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d07c76/32.png) [@chauk-mean](https://www.ruby-forum.com/u/chauk-mean)\
**Post date:** [June 16, 2009, 11:38pm UTC](https://www.ruby-forum.com/t/extracting-and-inserting-a-column-from-into-an-array/170365/3 "2009-06-16T23:38:44Z")

</div>

Hi Matt,

> result = img\_data.enum\_slice(4).map {|arr| arr[3]}
> 
> result = img\_data.enum\_slice(3).zip(alpha\_data).flatten

This is exactly what I was looking for. I just replace enum\_slice with  
each\_slice as I’m using ruby 1.9.  
Thanks a lot.

Cheers.

Chauk-Mean.

---

<div class="post-metadata">

**Author:** ![chauk-mean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d07c76/32.png) [@chauk-mean](https://www.ruby-forum.com/u/chauk-mean)\
**Post date:** [June 17, 2009, 2:50am UTC](https://www.ruby-forum.com/t/extracting-and-inserting-a-column-from-into-an-array/170365/4 "2009-06-17T02:50:52Z")

</div>

On Jun 16, 9:34 am, Chauk-Mean P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =\> [“a1”, “a2”] # the array of every four elements i.e. alpha data  
> =\> [“r1”, “g1”, “b1”, “a1”, "r2, “g2”, “b2”, “a2”]
> 
> It’s like inserting a column to a matrix of n rows and 3 columns.
> 
> I’m wondering if there is an easy or seamless way to do these  
> operations.

Possibly not as efficient, but if you were representing the data as a  
‘2D’ array (array of arrays) alternatively you could .transpose the  
array, insert a row, and .transpose it back.
