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)
But the problem is that i dont know how much element will be populated
in my array every time the program runs and the index value will also
change.
So i need to dynamically pass the index value and this should delete the
values pertaining to the index
Surjit N. wrote:
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)
But the problem is that i dont know how much element will be populated
in my array every time the program runs and the index value will also
change.
So i need to dynamically pass the index value and this should delete the
values pertaining to the index
You can’t delete by index if you don’t have an index, so I assume your
program will have the index in some form or another. Perhaps in a
variable?
a.delete(some_index) # where some_index contains an integer
If you post more of your program we’ll be better able to help you.
Surjit N. 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)
But the problem is that i dont know how much element will be populated
in my array every time the program runs and the index value will also
change.
So i need to dynamically pass the index value and this should delete the
values pertaining to the index
you could try something like…
a.each_with_index do |item, index|
a.delete_at(index) if item.eql?(something)
end
~Jeremy
On Nov 7, 2007 10:02 PM, Surjit N. [email protected]
wrote:
I can use the command
a.delete_at(0)
a.delete_at(4)
a.delete_at(6)
But the problem is that i dont know how much element will be populated
in my array every time the program runs and the index value will also
change.
If you really want to delete a few indexes you can build a new array:
b = []
a.each_with_index {|e, i| b<< e if [0, 4, 6].include?(i)}
a=b
If i have an array
a = [1,2,3,4]
The default index of 1 is 0
so we use a.delete_at(0) to delete 1
See my Q’s again
Jeremy W. wrote:
Surjit N. 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)
But the problem is that i dont know how much element will be populated
in my array every time the program runs and the index value will also
change.
So i need to dynamically pass the index value and this should delete the
values pertaining to the index
you could try something like…
a.each_with_index do |item, index|
a.delete_at(index) if item.eql?(something)
end
~Jeremy
I think you want Array#delete_if.
a.delete_if {|v| v == 1}
deletes all entries in a that are 1.
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.
On Nov 7, 2007 10:02 PM, Surjit N. [email protected]
wrote:
I can use the command
a.delete_at(0)
a.delete_at(4)
a.delete_at(6)
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
Surjit N. 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
smeitei
November 7, 2007, 10:04pm
10
On Nov 7, 2007, at 1:40 PM, Surjit N. wrote:
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).
Blessings,
TwP
smeitei
November 8, 2007, 2:08am
12
On Nov 7, 2007 3:00 PM, Tim P. [email protected] wrote:
Array1 = [1,2,3,4,7,4]
Blessings,
TwP
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
smeitei
November 8, 2007, 4:48am
13
On Nov 7, 2007, at 1:40 PM, Surjit N. wrote:
cfp:~ > cat a.rb
array = 1, 2, 3, 4, 7, 4
index = 1, 3
i = -1
array.delete_if{ index.delete(i+=1) }
p array #=> [1, 3, 7, 4]
index = -2, -1
i = -(array.size + 1)
array.delete_if{ index.delete(i+=1) }
p array #=> [1, 3]
cfp:~ > ruby a.rb
[1, 3, 7, 4]
[1, 3]
this approach avoids any sorting or extra copying and also does the
deletion in one pass.
regards.
a @ http://codeforpeople.com/
smeitei
November 8, 2007, 5:18am
14
On Nov 7, 2007 10:05 PM, Todd B. [email protected] wrote:
On Nov 7, 2007 9:47 PM, ara.t.howard [email protected] wrote:
has nothing to do with the Array’s ordering, so I was a little
surprised at this assumption.
Todd
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
smeitei
November 8, 2007, 5:15am
15
On Nov 7, 2007 9:47 PM, ara.t.howard [email protected] wrote:
array.delete_if{ index.delete(i+=1) }
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
smeitei
November 8, 2007, 5:41am
16
On Nov 7, 2007, at 9:11 PM, Todd B. 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.
cheers.
a @ http://codeforpeople.com/
smeitei
November 8, 2007, 6:20am
17
On Behalf Of Surjit N.
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
=> nil
array = [1,2,3,4,7,4]
=> [1, 2, 3, 4, 7, 4]
del=[1,3]
=> [1, 3]
array.delete_atx del
=> [1, 3]
array
=> [1, 3, 4, 4]
kind regards -botp
smeitei
November 8, 2007, 8:31am
18
On 07.11.2007 21:02, Surjit N. wrote:
I can use the command
a.delete_at(0)
a.delete_at(4)
a.delete_at(6)
But the problem is that i dont know how much element will be populated
in my array every time the program runs and the index value will also
change.
So i need to dynamically pass the index value and this should delete the
values pertaining to the index
I am surprised nobody mentioned slice - just apply indexes in decreasing
order:
irb(main):012:0> a = [1,4,6,7,9,8]
=> [1, 4, 6, 7, 9, 8]
irb(main):013:0> a.slice! 2,1
=> [6]
irb(main):014:0> a
=> [1, 4, 7, 9, 8]
irb(main):015:0> a.slice! 1,1
=> [4]
irb(main):016:0> a
=> [1, 7, 9, 8]
Kind regards
robert
smeitei
November 8, 2007, 6:42am
19
On Nov 8, 2007 5:40 AM, Surjit N. [email protected]
wrote:
but this doesn’t work
Array1 = [1,2,3,4,7,4]
del = [1,3]
keep = []
(0…Array1.length).each do |x|
keep << Array1[x] unless del.include?(x)
end
p keep #> [1, 3, 7, 4]
Harry
smeitei
November 8, 2007, 3:03pm
20
I got it working now…Thanks a lot everyone for all the suggestions…