Tim Pease
11/7/2007 9:01:00 PM
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).
Blessings,
TwP