how to delete array - RUBY
This is a discussion on how to delete array - RUBY ; 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 ...
-
how to delete array
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-forum.com/.
-
Re: how to delete array
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-forum.com/.
-
Re: how to delete array
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-forum.com/.
-
Re: how to delete array
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-forum.com/.
-
Re: how to delete array
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
-
Re: how to delete array
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
-
Re: how to delete array
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-forum.com/.
-
Re: how to delete array
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-forum.com/.
-
Re: how to delete array
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-forum.com/.
-
Re: how to delete array
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
Similar Threads
-
By Application Development in forum RUBY
Replies: 4
Last Post: 11-09-2007, 05:47 AM
-
By Application Development in forum c++
Replies: 18
Last Post: 10-10-2007, 05:59 AM
-
By Application Development in forum RUBY
Replies: 15
Last Post: 08-18-2007, 10:16 PM
-
By Application Development in forum c++
Replies: 3
Last Post: 08-17-2007, 02:15 AM
-
By Application Development in forum REXX
Replies: 2
Last Post: 11-10-2006, 01:55 PM