Looping through array, deleting elements
- From: "dkmd_nielsen" <donn@xxxxxxxxxx>
- Date: 8 Mar 2007 12:40:57 -0800
My idea of looping through an array, interrogating elements that are
of class instruction, and deleting the element does not appear to work
as I thought. I think I know why. What is the best solution?
block.each {|i| ab.delete(i) if i.parm == "FOO"}
where block is a series of elements of class instruction(i). Class
instruction has two attributes, parm and arg. If the instruction parm
is FOO, then delete the instruction object from the array and keep
looping. Half of what should be deleted actually get's deleted. I
think the reason is: the loop is looking at element x foo, element x
foo get's deleted, the array is shifted left, array pointer has not
changed but is now referencing element y foo, loop advances the array
pointer, and now array pointer is pointing at element z foo.
If what I think is correct, what is the best solution? Do I set the
current element to nil, then compact the array after the loop? Do I
delete the instruction and then do a redo? Or is there a better
solution than those?
Thanks,
dvn
.
- Follow-Ups:
- Re: Looping through array, deleting elements
- From: Tim Pease
- Re: Looping through array, deleting elements
- From: Stefano Crocco
- Re: Looping through array, deleting elements
- From: Farrel Lifson
- Re: Looping through array, deleting elements
- From: Tim Hunter
- Re: Looping through array, deleting elements
- Prev by Date: rdoc and and single file output
- Next by Date: Re: Perl 6 (Was: Boy I love the Ruby community)
- Previous by thread: rdoc and and single file output
- Next by thread: Re: Looping through array, deleting elements
- Index(es):
Relevant Pages
|