Mostly when I have a problem like this, I find that the indexes I want to
delete are a list generated by some property of the elements. ie. perhaps
you want to delete indexes 0, 4, 6 because the elements at those indexes are
(say) larger than three.
In that case, there's a much cleaner way:
a.reject!{|e| e > 3}
If you can say how you calculate that list of indexes to delete, we may be
able to help better.
Les
···
On Nov 7, 2007 10:02 PM, Surjit Nameirakpam <surjit.meitei@gmail.com> wrote:
Problem
consider an array
a = [1,4,6,7,9,8]
I want to delete the array value w.r.t index
e.g i want to delte 0,4,6 element in one go
I can use the command
a.delete_at(0)
a.delete_at(4)
a.delete_at(6)
Array1 = [1,2,3,4,7,4]
del = [1,3]
del.sort.reverse.each {|index| Array1.delete_at(index)}
You need to do the sort.reverse trick so that you don't change the size of Array1 and then try to delete one of the larger indicies. WIth sort.reverse, you'll always be deleting the largest index first.
I know you can pass a block to sort to reverse the order, but sort.reverse is a little clearer (although less efficient).
Blessings,
TwP
···
On Nov 7, 2007, at 1:40 PM, Surjit Nameirakpam wrote:
Surjit Nameirakpam wrote:
My business logic doesn't help me find which values i have to delete but
i will know what are the indexes i have to delete.
Actually i have collected the indexes i have to delete in an array
e.g
Array1 = [1,2,3,4,7,4]
indexes to be delted is collected in an array del=[1,3] ..i.e i should
delete 2 and 4 values
On Behalf Of Surjit Nameirakpam
# Array1 = [1,2,3,4,7,4]
# indexes to be delted is collected in an array del=[1,3] ..i.e
# i should
# delete 2 and 4 values
# i tried using
# Array1.delete_at(del)
# but this doesn't work
loop thru your indexes to the array, ie,
array = [1,2,3,4,7,4]
=> [1, 2, 3, 4, 7, 4]
del=[1,3]
=> [1, 3]
del.each{|i| array.delete_at(i)}
=> [1, 3]
array
=> [1, 3, 4, 4]
or you can then create your own fancy delete_at method
class Array
def delete_atx(d)
d.each{|i| self.delete_at(i)}
end
end
This is good unless they have an index in del that happens outside of
the size of the array.
If they can guarantee a value that should never occur in the original,
a better way (though more wordy) might be...
v = value_that_never_occurs = nil
array1 = 1 ,2 ,3 ,4 ,7 ,4
indices = 1, 3
indices.each { |i| array1[i] = v }
array1.delete(nil)
p array1
just another thought,
Todd
···
On Nov 7, 2007 3:00 PM, Tim Pease <tim.pease@gmail.com> wrote:
On Nov 7, 2007, at 1:40 PM, Surjit Nameirakpam wrote:
> Surjit Nameirakpam wrote:
>> My business logic doesn't help me find which values i have to
>> delete but
>> i will know what are the indexes i have to delete.
>
> Actually i have collected the indexes i have to delete in an array
>
> e.g
>
> Array1 = [1,2,3,4,7,4]
>
> indexes to be delted is collected in an array del=[1,3] ..i.e i should
> delete 2 and 4 values
>
> i tried using
>
> Array1.delete_at(del)
>
Array1 = [1,2,3,4,7,4]
del = [1,3]
del.sort.reverse.each {|index| Array1.delete_at(index)}
You need to do the sort.reverse trick so that you don't change the
size of Array1 and then try to delete one of the larger indicies.
WIth sort.reverse, you'll always be deleting the largest index first.
I know you can pass a block to sort to reverse the order, but
sort.reverse is a little clearer (although less efficient).
This is excellent! Only, what if your underlying implementation does
not traverse an array in order with #delete_if ? As it turns out, it
does, but would it have to? I see #delete_if as a conditional that
has nothing to do with the Array's ordering, so I was a little
surprised at this assumption.
Todd
···
On Nov 7, 2007 9:47 PM, ara.t.howard <ara.t.howard@gmail.com> wrote:
On Nov 7, 2007, at 1:40 PM, Surjit Nameirakpam wrote:
> Surjit Nameirakpam wrote:
>> My business logic doesn't help me find which values i have to
>> delete but
>> i will know what are the indexes i have to delete.
>
> Actually i have collected the indexes i have to delete in an array
>
> e.g
>
> Array1 = [1,2,3,4,7,4]
>
> indexes to be delted is collected in an array del=[1,3] ..i.e i should
> delete 2 and 4 values
>
> i tried using
>
> Array1.delete_at(del)
>
> but this doesn't work
> --
> Posted via http://www.ruby-forum.com/\.
>
cfp:~ > cat a.rb
array = 1, 2, 3, 4, 7, 4
index = 1, 3
i = -1
array.delete_if{ index.delete(i+=1) }
Nevermind that. I can see how you might want to consider position in
a condition (maybe state machine stuff or whatever). #delete_if is
after all an Array method, which is the only set of objects that
require a linear order. Sorry for noise now.
Todd
···
On Nov 7, 2007 10:05 PM, Todd Benson <caduceass@gmail.com> wrote:
On Nov 7, 2007 9:47 PM, ara.t.howard <ara.t.howard@gmail.com> wrote:
This is excellent! Only, what if your underlying implementation does
not traverse an array in order with #delete_if ? As it turns out, it
does, but would it have to? I see #delete_if as a conditional that
has nothing to do with the Array's ordering, so I was a little
surprised at this assumption.
no that's really a valid concern i think - happens to be that #delete_if is defined in terms of #each (from Enumerable) so i know it's in order but once indeed needs to assume/know that for it to work.
Nevermind that. I can see how you might want to consider position in
a condition (maybe state machine stuff or whatever). #delete_if is
after all an Array method, which is the only set of objects that
require a linear order. Sorry for noise now.
--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama
Okay, I'll take note of that. I suggested that an Array was the only
object required to hold a set of other objects in order. No, that
methodology comes from the Enumerable mixin. Duh, I should be so
stupid. In other words, we should be aware that "successive" order is
something that holds for something that mixes in Enumerable no matter
what the implementation. The fact that #delete_if doesn't exist in
the Enumerable module is what threw me off.
Also, I suppose we should pay attention to the fact that the action of
"deleting if" applies to objects that are not necessarily in order
(i.e. Hash). Hmm... I think this has been discussed before.
thx ara,
Todd
···
On Nov 7, 2007 10:39 PM, ara.t.howard <ara.t.howard@gmail.com> wrote:
On Nov 7, 2007, at 9:11 PM, Todd Benson wrote:
> Nevermind that. I can see how you might want to consider position in
> a condition (maybe state machine stuff or whatever). #delete_if is
> after all an Array method, which is the only set of objects that
> require a linear order. Sorry for noise now.
no that's really a valid concern i think - happens to be that #delete_if is defined in terms of #each (from Enumerable) so i know
it's in order but once indeed needs to assume/know that for it to work.