[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Delete array element within iterator block?

Jari Williamsson

11/30/2007 11:51:00 AM

How do I (in an elegant way) delete an element of an array within a
block, while using a block iterator?

The code below won't work (the wanted behavior here is to get all array
elements to print and but also delete the 2nd element after it has been
encountered):
---
arr = [1,2,3,4]
arr.each_with_index { |e, i|
p e
arr.delete_at(i) if i == 1
}
---
results in:
1
2
4

Of curse, I could loop and keep track of the index manually, but I'm
mainly checking if there is any built-in stuff here.


Best regards,

Jari Williamsson

3 Answers

David and Sharon Phillips

11/30/2007 12:17:00 PM

0


On 30/11/2007, at 10:51 PM, Jari Williamsson wrote:

> How do I (in an elegant way) delete an element of an array within a
> block, while using a block iterator?
I assume using delete_if is no good in this situation?
[1,2,3,4,5,6].delete_if{|i| i>4}
> [1,2,3,4]

>
>
> The code below won't work (the wanted behavior here is to get all
> array elements to print and but also delete the 2nd element after it
> has been encountered):
> ---
> arr = [1,2,3,4]
> arr.each_with_index { |e, i|
> p e
> arr.delete_at(i) if i == 1
> }
> ---
> results in:
> 1
> 2
> 4
This is interesting, because although the results printed on the
screen imply the 3 was removed, if you look at the array afterthe
operation you'll see it was the 2 removed (as we wanted)

irb(main):013:0> (a=[1,2,3,4]).each_with_index{|e,i| p
e;a.delete_at(i) if i==1}
1
2
4
=> [1, 3, 4]

I've no ideas why

Cheers,
Dave

>
>
> Of curse, I could loop and keep track of the index manually, but I'm
> mainly checking if there is any built-in stuff here.
>
>
> Best regards,
>
> Jari Williamsson
>


David A. Black

11/30/2007 1:17:00 PM

0

HI --

On Fri, 30 Nov 2007, Sharon Phillips wrote:

>
> On 30/11/2007, at 10:51 PM, Jari Williamsson wrote:
>
>> How do I (in an elegant way) delete an element of an array within a block,
>> while using a block iterator?
> I assume using delete_if is no good in this situation?
> [1,2,3,4,5,6].delete_if{|i| i>4}
>> [1,2,3,4]
>
>>
>>
>> The code below won't work (the wanted behavior here is to get all array
>> elements to print and but also delete the 2nd element after it has been
>> encountered):
>> ---
>> arr = [1,2,3,4]
>> arr.each_with_index { |e, i|
>> p e
>> arr.delete_at(i) if i == 1
>> }
>> ---
>> results in:
>> 1
>> 2
>> 4
> This is interesting, because although the results printed on the screen imply
> the 3 was removed, if you look at the array afterthe operation you'll see it
> was the 2 removed (as we wanted)
>
> irb(main):013:0> (a=[1,2,3,4]).each_with_index{|e,i| p e;a.delete_at(i) if
> i==1}
> 1
> 2
> 4
> => [1, 3, 4]
>
> I've no ideas why

First time through:

a => [1,2,3,4]
e => 1
i => 0

Second time through:

a => [1,2,3,4]
e => 2
i => 1

Third time through:

a => [1,3,4]
e => 4 # i.e., a[2]
i => 2

Or some such thing. Of course the best idea is never to do this :-)


David

--
Upcoming training by David A. Black/Ruby Power and Light, LLC:
* Intro to Rails, London, UK, December 3-6 (by Skills Matter)
See http://www.r... for details and 2008 announcements!

Jari Williamsson

11/30/2007 1:47:00 PM

0

Sharon Phillips wrote:

> On 30/11/2007, at 10:51 PM, Jari Williamsson wrote:
>
>> How do I (in an elegant way) delete an element of an array within a
>> block, while using a block iterator?
> I assume using delete_if is no good in this situation?
> [1,2,3,4,5,6].delete_if{|i| i>4}
> > [1,2,3,4]

Thanks, delete_if works just fine!


Best regards,

Jari Williamsson