[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Iterate through array and delete if match

jackster the jackle

10/4/2008 12:10:00 AM

I'm trying to delete elements of an array if they match a string but my
code always leaves some matches and I think it's because it's having
trouble iterating through the same array it is trying to delete from, is
that true?

Here is the code:

@acl_all_array.each do |range|
if range[/access-list/]
@acl_all_array.delete(range)
puts range
end
end

Is this the correct way to delete matched entries from an array?

Thanks

John
--
Posted via http://www.ruby-....

7 Answers

Tim Hunter

10/4/2008 12:18:00 AM

0

jackster the jackle wrote:
> I'm trying to delete elements of an array if they match a string but my
> code always leaves some matches and I think it's because it's having
> trouble iterating through the same array it is trying to delete from, is
> that true?
>
> Here is the code:
>
> @acl_all_array.each do |range|
> if range[/access-list/]
> @acl_all_array.delete(range)
> puts range
> end
> end
>
> Is this the correct way to delete matched entries from an array?
>
> Thanks
>
> John

Use delete_if instead.

--
RMagick: http://rmagick.ruby...

Shawn Anderson

10/4/2008 12:37:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

I believe there is a method on Array called delete_if for just such a case..
IIRC

/Shawn

On Fri, Oct 3, 2008 at 5:18 PM, Tim Hunter <TimHunter@nc.rr.com> wrote:

> jackster the jackle wrote:
>
>> I'm trying to delete elements of an array if they match a string but my
>> code always leaves some matches and I think it's because it's having
>> trouble iterating through the same array it is trying to delete from, is
>> that true?
>>
>> Here is the code:
>>
>> @acl_all_array.each do |range|
>> if range[/access-list/]
>> @acl_all_array.delete(range)
>> puts range
>> end
>> end
>>
>> Is this the correct way to delete matched entries from an array?
>>
>> Thanks
>>
>> John
>>
>
> Use delete_if instead.
>
> --
> RMagick: http://rmagick.ruby...
>
>

ragav

10/4/2008 12:42:00 AM

0

Tim Hunter wrote:
> jackster the jackle wrote:
>> puts range
>> end
>> end
>>
>> Is this the correct way to delete matched entries from an array?
>>
>> Thanks
>>
>> John
>
> Use delete_if instead.

Or Array#reject!

deleting while iterating with each is undefined behavior. It's like
sawing off the tree branch you are sitting on.
--
Posted via http://www.ruby-....

jackster the jackle

10/4/2008 1:01:00 AM

0

> Or Array#reject!
>
> deleting while iterating with each is undefined behavior. It's like
> sawing off the tree branch you are sitting on.


hehehe...I know what you mean!

Tim's solution worked, I used:

@acl_all_array.each do |range|
if range[/access-list/]
@acl_range.push(range)
end
end
@acl_all_array.delete_if { |x| x[/access-list/] }

Thanks for all the help guys...I am going to read up on Array#reject! to
see how that might help me.

john

--
Posted via http://www.ruby-....

David A. Black

10/4/2008 3:01:00 AM

0

Hi --

On Sat, 4 Oct 2008, jackster the jackle wrote:

>> Or Array#reject!
>>
>> deleting while iterating with each is undefined behavior. It's like
>> sawing off the tree branch you are sitting on.
>
>
> hehehe...I know what you mean!
>
> Tim's solution worked, I used:
>
> @acl_all_array.each do |range|
> if range[/access-list/]
> @acl_range.push(range)
> end
> end
> @acl_all_array.delete_if { |x| x[/access-list/] }
>
> Thanks for all the help guys...I am going to read up on Array#reject! to
> see how that might help me.

Check out Array#partition too. What you've got there looks like it
could be:

@acl_all_array, @acl_range = @acl_all_array.partition do |range|
range[/access-list/]
end

or something like that.


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.r... for details and updates!

jackster the jackle

10/4/2008 10:26:00 AM

0

David A. Black wrote:
> Hi --
>
> Check out Array#partition too. What you've got there looks like it
> could be:
>
> @acl_all_array, @acl_range = @acl_all_array.partition do |range|
> range[/access-list/]
> end
>
> or something like that.


good call David.....I'm going to try and incorporate that into my code.
Nice to see you on the forum! I'm still mad that my training was never
approved :-(

john

--
Posted via http://www.ruby-....

Robert Klemme

10/5/2008 4:25:00 PM

0

On 04.10.2008 12:26, jackster the jackle wrote:
> David A. Black wrote:
>> Hi --
>>
>> Check out Array#partition too. What you've got there looks like it
>> could be:
>>
>> @acl_all_array, @acl_range = @acl_all_array.partition do |range|
>> range[/access-list/]
>> end
>>
>> or something like that.

And, to give you another option you can fill the second array inside the
delete_if block as well:

@acl_all_array.delete_if { |x| x[/access-list/] and
@acl_range.push(x)}

Cheers

robert