Array deletion


#1

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


#2

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

robert


#3

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.


#4

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


#5

Hi Park,

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

Thanks all,

P.Raveendran


#6

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.


#7

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