array manipulation

A

Analogy Analogy

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!
 
S

SpringFlowers AutumnMoon

Analogy said:
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
 
R

Robert Dober

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!
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
 
P

paul

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>
 

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
473,774
Messages
2,569,598
Members
45,159
Latest member
SweetCalmCBDGummies
Top