Forum: Ruby Iterate through array and delete if match

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.
B28a3426b4a8ae5ad2302fdf1c6674e8?d=identicon&s=25 Jackster The jackle (jackster)
on 2008-10-04 02:11
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
3afd3e5e05dc9310c89aa5762cc8dd1d?d=identicon&s=25 Tim Hunter (Guest)
on 2008-10-04 02:20
(Received via mailing list)
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.
Fc8263f8baca7a7b76605cfdd0c4afa2?d=identicon&s=25 Ragav Satish (ragav)
on 2008-10-04 02:43
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.
B28a3426b4a8ae5ad2302fdf1c6674e8?d=identicon&s=25 Jackster The jackle (jackster)
on 2008-10-04 03:02
> 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
81b5e41ae80dc7f1aa57d2bf40e7e7d0?d=identicon&s=25 Shawn Anderson (Guest)
on 2008-10-04 03:08
(Received via mailing list)
I believe there is a method on Array called delete_if for just such a
case..
IIRC

/Shawn
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2008-10-04 05:40
(Received via mailing list)
Hi --

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

> @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
B28a3426b4a8ae5ad2302fdf1c6674e8?d=identicon&s=25 Jackster The jackle (jackster)
on 2008-10-04 12:27
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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-10-05 18:26
(Received via mailing list)
On 04.10.2008 12:26, jackster the jackle wrote:
>> 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
This topic is locked and can not be replied to.