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>
Two successive delete_at methods will do what you ask with the c array
modified:
c=[[], [], [“BRKR”, “2008-09-09, 11:08:28”, “ISLAND”, “-200”,“16.1000”,
“3,220.00”, “-1.00”, “3,446.688”, “-1.0704”, “0.00”, “”],[]]
c.delete_at(0)
c.delete_at(0)
c
Perhaps you can get the essential info you need to manipulate with this:
c.delete([])
c.flatten!
I am not precisely sure why, but you cannot successfully chain either of
the these two statements:
c.delete_at(0).delete_at(0)
c.delete([]).flatten!
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”]
Junkone wrote:
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
On Thu, Sep 11, 2008 at 2:50 PM, DanDiebolt.exe [email protected]
wrote:
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
Junkone wrote:
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”, “”]]