[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how to delete array

Surjit Nameirakpam

11/7/2007 8:03:00 PM

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
--
Posted via http://www.ruby-....

37 Answers

Daniel Waite

11/7/2007 8:07:00 PM

0

Surjit Nameirakpam 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.
--
Posted via http://www.ruby-....

Surjit Nameirakpam

11/7/2007 8:14:00 PM

0

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
--
Posted via http://www.ruby-....

Jeremy Woertink

11/7/2007 8:16:00 PM

0

Surjit Nameirakpam 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
--
Posted via http://www.ruby-....

Leslie Viljoen

11/7/2007 8:19:00 PM

0

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

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)
>
> 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




--
[we need your code-fu] : www.zadic.co.za

Leslie Viljoen

11/7/2007 8:24:00 PM

0

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

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)


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


--
[we need your code-fu] : www.zadic.co.za

Tim Hunter

11/7/2007 8:29:00 PM

0

Jeremy Woertink wrote:
> Surjit Nameirakpam 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.
--
Posted via http://www.ruby-....

Surjit Nameirakpam

11/7/2007 8:36:00 PM

0

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.
--
Posted via http://www.ruby-....

Surjit Nameirakpam

11/7/2007 8:40:00 PM

0

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

Tim Pease

11/7/2007 9:01:00 PM

0

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

Surjit Nameirakpam

11/8/2007 12:31:00 AM

0

F5
--
Posted via http://www.ruby-....