# Array manipulation

**URL:** <https://www.ruby-forum.com/t/array-manipulation/115464>\
**Category:** Ruby\
**Created:** [September 25, 2007, 4:36pm UTC](https://www.ruby-forum.com/t/array-manipulation/115464 "2007-09-25T16:36:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![analogy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@analogy](https://www.ruby-forum.com/u/analogy)\
**Post date:** [September 25, 2007, 4:36pm UTC](https://www.ruby-forum.com/t/array-manipulation/115464/1 "2007-09-25T16:36:36Z")

</div>

Hi Folks,

I have a general newbie type question  
Say I have a 2D array that I obtained from an external program:

array = [[“a”, “b”, “”, “c”, “5”], [“d”, “e”, “f”, “9.0”, “7”, “g”, “”,  
“”, “”], ["", “h”, “5”, “”, “”]]

Is there a way to remove just the last two empty strings ("") in the  
last element, so the array will look like this:

array = [[“a”, “b”, “”, “c”, “5”], [“d”, “e”, “f”, “9.0”, “7”, “g”, “”,  
“”, “”], ["", “h”, “5”]]

I’ve looked at array.compact, but this removes all of the empty  
strings(""), plus I’d have to flatten the array to 1D, which is no good.  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![analogy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@analogy](https://www.ruby-forum.com/u/analogy)\
**Post date:** [September 25, 2007, 5:05pm UTC](https://www.ruby-forum.com/t/array-manipulation/115464/2 "2007-09-25T17:05:13Z")

</div>

Analogy A. wrote:

> Hi Folks,
> 
> I have a general newbie type question  
> Say I have a 2D array that I obtained from an external program:
> 
> array = [[“a”, “b”, “”, “c”, “5”], [“d”, “e”, “f”, “9.0”, “7”, “g”, “”,  
> “”, “”], ["", “h”, “5”, “”, “”]]
> 
> Is there a way to remove just the last two empty strings ("") in the  
> last element, so the array will look like this:
> 
> array = [[“a”, “b”, “”, “c”, “5”], [“d”, “e”, “f”, “9.0”, “7”, “g”, “”,  
> “”, “”], ["", “h”, “5”]]

you can use array[2][3,2] = nil to delete the last 2 elements  
and use flatten to get a new array

---

<div class="post-metadata">

**Author:** ![analogy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@analogy](https://www.ruby-forum.com/u/analogy)\
**Post date:** [September 25, 2007, 5:13pm UTC](https://www.ruby-forum.com/t/array-manipulation/115464/3 "2007-09-25T17:13:53Z")

</div>

On 9/25/07, Analogy A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> last element, so the array will look like this:

2.times{ array.last.pop }

but is this really what you want? I mean are you not up to something  
more general?  
for example:  
2.times{ array.last.pop if array.last.empty? }  
do you see what I mean?

Cheers  
Robert

---

<div class="post-metadata">

**Author:** ![analogy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@analogy](https://www.ruby-forum.com/u/analogy)\
**Post date:** [September 25, 2007, 5:24pm UTC](https://www.ruby-forum.com/t/array-manipulation/115464/4 "2007-09-25T17:24:54Z")

</div>

> Say I have a 2D array that I obtained from an external program:  
> “7”, “g”, “”,  
> “”, “”], [“”, “h”, “5”]]
> 
> ## I’ve looked at array.compact, but this removes all of the empty strings(“”), plus I’d have to flatten the array to 1D, which is no good. Thanks in advance!
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

If I understand you right and all you want to do is to remove empty  
strings  
from the end of the last array in an array of arrays, this should  
suffice:

irb(main):001:0\> a = [[“a”, “b”, “”, “c”, “5”], [“d”, “e”, “f”, “9.0”,  
“7”,  
“g”, “”, “”, “”], [“”, “h”, “5”, “”, “”]]  
=\> [[“a”, “b”, “”, “c”, “5”], [“d”, “e”, “f”, “9.0”, “7”, “g”, “”, “”,  
“”],  
[“”, “h”, “5”, “”, “”]]  
irb(main):002:0\> while a.last.last == “” do  
irb(main):003:1\* a.last.pop  
irb(main):004:1\> end  
=\> nil  
irb(main):005:0\> a  
=\> [[“a”, “b”, “”, “c”, “5”], [“d”, “e”, “f”, “9.0”, “7”, “g”, “”, “”,  
“”],  
[“”, “h”, “5”]]  
irb(main):006:0\>

HTH,

Felix

---

<div class="post-metadata">

**Author:** ![analogy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@analogy](https://www.ruby-forum.com/u/analogy)\
**Post date:** [September 25, 2007, 6:06pm UTC](https://www.ruby-forum.com/t/array-manipulation/115464/5 "2007-09-25T18:06:03Z")

</div>

On Tue, 2007-09-25 at 23:36 +0900, Analogy A. wrote:

> Hi Folks,
> 
> I have a general newbie type question  
> Say I have a 2D array that I obtained from an external program:
> 
> array = [[“a”, “b”, “”, “c”, “5”], [“d”, “e”, “f”, “9.0”, “7”, “g”, “”,  
> “”, “”], ["", “h”, “5”, “”, “”]]

this any good?  
i’d assumed you just wanted to get rid of the two empty strings as  
they’re duplicates…

irb(main):015:0\> arr = [[‘paul’,‘fox’,’’,’’],[‘testing’,‘123’,’’,’’]]  
=\> [[“paul”, “fox”, “”, “”], [“testing”, “123”, “”, “”]]  
irb(main):016:0\> arr.map{|a| pp a.uniq}  
[“paul”, “fox”, “”]  
[“testing”, “123”, “”]  
=\> [nil, nil]  
irb(main):017:0\>
