how to delete array

  • Thread starter Surjit Nameirakpam
  • Start date
S

Surjit Nameirakpam

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
 
D

Daniel Waite

Surjit said:
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.
 
S

Surjit Nameirakpam

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
 
J

Jeremy Woertink

Surjit said:
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
 
L

Leslie Viljoen

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

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.


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
 
L

Leslie Viljoen

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

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)


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
 
T

Tim Hunter

Jeremy said:
Surjit said:
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.
 
S

Surjit Nameirakpam

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

Surjit Nameirakpam

Surjit said:
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
 
T

Tim Pease

Surjit said:
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).

Blessings,
TwP
 
T

Todd Benson

Surjit said:
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).

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 = v }
array1.delete(nil)
p array1

just another thought,
Todd
 
A

ara.t.howard

Surjit said:
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

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/
 
T

Todd Benson

Surjit said:
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

cfp:~ > cat a.rb
array = 1, 2, 3, 4, 7, 4

index = 1, 3
i = -1
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
 
T

Todd Benson

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

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
 
A

ara.t.howard

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

Peña, Botp

T24gQmVoYWxmIE9mIFN1cmppdCBOYW1laXJha3BhbQ0KIyBBcnJheTEgPSBbMSwyLDMsNCw3LDRd
DQojIGluZGV4ZXMgdG8gYmUgZGVsdGVkIGlzIGNvbGxlY3RlZCBpbiBhbiBhcnJheSBkZWw9WzEs
M10gLi5pLmUgDQojIGkgc2hvdWxkIA0KIyBkZWxldGUgMiBhbmQgNCB2YWx1ZXMNCiMgaSB0cmll
ZCB1c2luZw0KIyBBcnJheTEuZGVsZXRlX2F0KGRlbFtdKQ0KIyBidXQgdGhpcyBkb2Vzbid0IHdv
cmsNCg0KbG9vcCB0aHJ1IHlvdXIgaW5kZXhlcyB0byB0aGUgYXJyYXksIGllLA0KDQo+IGFycmF5
ID0gWzEsMiwzLDQsNyw0XQ0KPT4gWzEsIDIsIDMsIDQsIDcsIDRdDQo+IGRlbD1bMSwzXQ0KPT4g
WzEsIDNdDQo+IGRlbC5lYWNoe3xpfCBhcnJheS5kZWxldGVfYXQoaSl9DQo9PiBbMSwgM10NCj4g
YXJyYXkNCj0+IFsxLCAzLCA0LCA0XQ0KDQpvciB5b3UgY2FuIHRoZW4gY3JlYXRlIHlvdXIgb3du
IGZhbmN5IGRlbGV0ZV9hdCBtZXRob2QNCg0KPiBjbGFzcyBBcnJheQ0KPiBkZWYgZGVsZXRlX2F0
eChkKQ0KPiAgIGQuZWFjaHt8aXwgc2VsZi5kZWxldGVfYXQoaSl9DQo+IGVuZA0KPiBlbmQNCj0+
IG5pbA0KPiBhcnJheSA9IFsxLDIsMyw0LDcsNF0NCj0+IFsxLCAyLCAzLCA0LCA3LCA0XQ0KPiBk
ZWw9WzEsM10NCj0+IFsxLCAzXQ0KPiBhcnJheS5kZWxldGVfYXR4IGRlbA0KPT4gWzEsIDNdDQo+
IGFycmF5DQo9PiBbMSwgMywgNCwgNF0NCg0Ka2luZCByZWdhcmRzIC1ib3RwDQo=
 
H

Harry Kakueki

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

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
 
R

Robert Klemme

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

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
 
T

Todd Benson

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/

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
 

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,270
Messages
2,571,350
Members
48,036
Latest member
nickwillsonn

Latest Threads

Top