Forum: Ruby Array deletion

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Raveendran .. (Guest)
on 2008-12-02 08:30
Hi All,

 have an array like,

a=["one","xcxc","two","dadasd","three","hgfhgfh"]

I want to delete the index of a[1],a[3] and so on...


Thanks in advance,
P.Raveendran
http://raveendran.wordpress.com
Robert K. (Guest)
on 2008-12-02 09:35
(Received via mailing list)
On 02.12.2008 07:25, jazzez ravi wrote:
> Hi All,
>
>  have an array like,
>
> a=["one","xcxc","two","dadasd","three","hgfhgfh"]
>
> I want to delete the index of a[1],a[3] and so on...

Use delete_at - and read the documentation. :-)

  robert
Vamsi K. (Guest)
on 2008-12-02 10:37
jazzez ravi wrote:
> Hi All,
>
>  have an array like,
>
> a=["one","xcxc","two","dadasd","three","hgfhgfh"]
>
> I want to delete the index of a[1],a[3] and so on...
>
>
> Thanks in advance,
> P.Raveendran
> http://raveendran.wordpress.com

array.delete_at(index). use this to delete particular indexes in an
array.
Raveendran .. (Guest)
on 2008-12-02 10:44
Hi Vamsi and Robert,

delete_at(index) is useful when need to delete one data. But i want to
delete series like 1,3,5,7. Currently i am using loop for it. Any other
good code for it.

I tried Array.delete_at(1,3,7,9) --> Like this



Thanks,
 P.Raveendran
 http://raveendran.wordpress.com
Heesob P. (Guest)
on 2008-12-02 10:53
(Received via mailing list)
2008/12/2 jazzez ravi <removed_email_address@domain.invalid>:
> Hi Vamsi and Robert,
>
> delete_at(index) is useful when need to delete one data. But i want to
> delete series like 1,3,5,7. Currently i am using loop for it. Any other
> good code for it.
>
> I tried Array.delete_at(1,3,7,9) --> Like this
>
>
If there is no duplicate data, you can use values_at like this

a = a - a.values_at(1,3,5,7)

Regards,

Park H.
Raveendran .. (Guest)
on 2008-12-02 11:01
Hi Park,

   Thanks for your code. I will catch u soon with another issue


Thanks all,

P.Raveendran
http://raveendran.wordpress.com
Pit C. (Guest)
on 2008-12-02 11:27
(Received via mailing list)
2008/12/2 Heesob P. <removed_email_address@domain.invalid>:
> If there is no duplicate data, you can use values_at like this
> (...)

Or apply Array#- to the indexes. This works with duplicate data, too:

  a.values_at(*((0...a.size).to_a - [1, 3, 5, 7]))

Regards,
Pit
This topic is locked and can not be replied to.