Re: how to delete array

ignore previous post (it does not work right)

maybe this one,

array = [1,2,3,4,7,4]
=> [1, 2, 3, 4, 7, 4]
del=[1,3]
=> [1, 3]

del.each{|i| array[i]=nil}
=> [1, 3]
array.compact
=> [1, 3, 7, 4]

-----Original Message-----

From: Peña, Botp

Sent: Thursday, November 08, 2007 1:00 PM

To: ‘[email protected]

Subject: RE: how to delete array

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