[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

delete from array while iterating

Tarscher

3/25/2009 4:48:00 PM

Hi all,

I want to delete an element from an array when it matches a condition.
But I first want to print something to the screen when a amùcth is
found.

I know a.delete_if but can I somehow add that print to the screen next
to the condition? If not , is there an alternative way to delete from
an array while iterating?

thanks
Stijn
4 Answers

RubyTalk@gmail.com

3/25/2009 5:00:00 PM

0

[2,3,4,5,6].delete_if{|x| (x%2=3D=3D0) && (puts x) }
2
4
6


On Wed, Mar 25, 2009 at 9:47 AM, Tarscher <tarscher@gmail.com> wrote:
> Hi all,
>
> I want to delete an element from an array when it matches a condition.
> But I first want to print something to the screen when a am=F9cth is
> found.
>
> I know a.delete_if but can I somehow add that print to the screen next
> to the condition? If not , is there an alternative way to delete from
> an array while iterating?
>
> thanks
> Stijn
>
>

Stefano Crocco

3/25/2009 5:04:00 PM

0

Alle Wednesday 25 March 2009, Tarscher ha scritto:
> Hi all,
>
> I want to delete an element from an array when it matches a condition.
> But I first want to print something to the screen when a am=F9cth is
> found.
>
> I know a.delete_if but can I somehow add that print to the screen next
> to the condition? If not , is there an alternative way to delete from
> an array while iterating?
>
> thanks
> Stijn

In the block you can do anything you want. Just make sure you return a true=
=20
value or a false value depending on whether the element should be removed o=
r=20
not. Here's an example:

a =3D [1,-1,-2,2,3]

a.delete_if do |i|
if i < 0
puts "#{i} will be deleted"
true
else false
end
end

This will return [1,2,3] and give the following output:

=2D1 will be deleted
=2D2 will be deleted

I hope this helps

Stefano


lasitha

3/25/2009 5:19:00 PM

0

On Wed, Mar 25, 2009 at 10:30 PM, RubyTalk@gmail.com <rubytalk@gmail.com> wrote:
> [2,3,4,5,6].delete_if{|x| (x%2==0) && (puts x) }
> 2
> 4
> 6

Err, that won't work. puts returns nil so the block will never return
true and none of the items will be removed.


We'd have to do:

arr.delete_if do |e|
if e.matches(condition)
puts e
true
end
end

Note: this can work w/o an else because the whole if expression will
return nil if none of the conditions match.

cheers,
lasitha

Robert Klemme

3/25/2009 5:24:00 PM

0

2009/3/25 Tarscher <tarscher@gmail.com>:
> I want to delete an element from an array when it matches a condition.
> But I first want to print something to the screen when a am=F9cth is
> found.
>
> I know a.delete_if but can I somehow add that print to the screen next
> to the condition? If not , is there an alternative way to delete from
> an array while iterating?

What makes you think you cannot print in the block of a delete_if?

irb(main):009:0> a =3D (1..5).to_a
=3D> [1, 2, 3, 4, 5]
irb(main):010:0> a.delete_if {|x| printf "Found %4d\n", x; x % 3 =3D=3D 0}
Found 1
Found 2
Found 3
Found 4
Found 5
=3D> [1, 2, 4, 5]
irb(main):011:0> a
=3D> [1, 2, 4, 5]

robert

--=20
remember.guy do |as, often| as.you_can - without end