delete row in array of arrays

J

Junkone

i have a array of arrays. i want to empty the first 2 child arrays.
how do i do it.
irb(#<Object:0x35208e8>):049:0> c=$stockTrades[0]
=> [[], [], ["BRKR", "2008-09-09, 11:08:28", "ISLAND", "-200",
"16.1000", "3,220.00", "-1.00", "3,446.688", "-1.0704", "0.00", ""],
[]]
irb(#<Object:0x35208e8>):050:0> c
=> [[], [], ["BRKR", "2008-09-09, 11:08:28", "ISLAND", "-200",
"16.1000", "3,220.00", "-1.00", "3,446.688", "-1.0704", "0.00", ""],
[]]
irb(#<Object:0x35208e8>):051:0> c[0]
=> []
irb(#<Object:0x35208e8>):052:0> c.delete(0)
=> nil
irb(#<Object:0x35208e8>):053:0>
 
S

Siep Korteling

Junkone said:
i have a array of arrays. i want to empty the first 2 child arrays.
how do i do it.

c=[[],[],["some_stuff"]]
c.slice!(0,2) # arguments are start and length
#or
c.slice!(0..1) # slice can handle a range as argument

Regards,

Siep
 
T

Todd Benson

FWIW, slice takes one bite out of the array whereas values_at can remove several bites. I mention this because it isn't clear if your application ultimately needs to remove or otherwise process the last element in the c array. You may well be facing a further step in dealing with the last item.

values_at can take an argument list of and arbitrary number of indices OR ranges.
a = %w{ a b c d e f }
=> ["a", "b", "c", "d", "e", "f"]
a.values_at(2..-1)
=> ["c", "d", "e", "f"]
a.values_at(2..-2)
=> ["c", "d", "e"]
a.values_at(2,-2)
=> ["c", "e"]

When I first read the original post, I thought it meant make the first
two elements empty arrays. Deleting the first two is easy, just
remember that slice! returns what you have deleted and not the new
array itself.

Todd
 
B

Bernard Kenik

Junkone said:
i have a array of arrays. i want to empty the first 2 child arrays.
how do i do it.
irb(#<Object:0x35208e8>):049:0> c=$stockTrades[0]
=> [[], [], ["BRKR", "2008-09-09, 11:08:28", "ISLAND", "-200",
"16.1000", "3,220.00", "-1.00", "3,446.688", "-1.0704", "0.00", ""],
[]]
irb(#<Object:0x35208e8>):050:0> c
=> [[], [], ["BRKR", "2008-09-09, 11:08:28", "ISLAND", "-200",
"16.1000", "3,220.00", "-1.00", "3,446.688", "-1.0704", "0.00", ""],
[]]
irb(#<Object:0x35208e8>):051:0> c[0]
=> []
irb(#<Object:0x35208e8>):052:0> c.delete(0)
=> nil
irb(#<Object:0x35208e8>):053:0>

c.delete(c[0])

will delete every element which equals c[0] in your case the result is
[["BRKR", "2008-09-09, 11:08:28", "ISLAND", "-200","16.1000",
"3,220.00", "-1.00", "3,446.688",-1.0704", "0.00", ""]]
 

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

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,652
Latest member
Campbellamy

Latest Threads

Top