[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

delete in Array (if exist) and count

Josselin

6/3/2007 2:19:00 PM

I am trying to delete an element from an array if it exists and count
the number of item afterwards

I wrote

anArray = [1,2,3,4,5,6,7,8,9]
anArray.size
=> 9
anArray.map {|e| e if e != 4}.compact
=> [1, 2, 3, 5, 6, 7, 8, 9]
anArray.map {|e| e if e != 4}.compact.size
=> 8


is ther a better way to do it... it it's enough ? (learning always how
to write better code..)

thanks

joss

5 Answers

Tim Hunter

6/3/2007 2:35:00 PM

0

Josselin wrote:
> I am trying to delete an element from an array if it exists and count
> the number of item afterwards
>
How about using Array#delete_if?

--
RMagick OS X Installer [http://rubyforge.org/project...]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?for...]
RMagick Installation FAQ [http://rmagick.rubyforge.org/instal...]


Todd Benson

6/3/2007 2:58:00 PM

0

On 6/3/07, Josselin <josselin@wanadoo.fr> wrote:
> I am trying to delete an element from an array if it exists and count
> the number of item afterwards
>
> I wrote
>
> anArray = [1,2,3,4,5,6,7,8,9]
> anArray.size
> => 9
> anArray.map {|e| e if e != 4}.compact
> => [1, 2, 3, 5, 6, 7, 8, 9]
> anArray.map {|e| e if e != 4}.compact.size
> => 8
>
>
> is ther a better way to do it... it it's enough ? (learning always how
> to write better code..)

Array#delete works also with no need for a block (i.e. you're only
checking for equality)

anArray = [1,2,3,4,5,6]
p anArray.delete(4)
p anArray.size

Josselin

6/3/2007 3:45:00 PM

0

On 2007-06-03 16:57:43 +0200, "Todd Benson" <caduceass@gmail.com> said:

> On 6/3/07, Josselin <josselin@wanadoo.fr> wrote:
>> I am trying to delete an element from an array if it exists and count
>> the number of item afterwards
>>
>> I wrote
>>
>> anArray = [1,2,3,4,5,6,7,8,9]
>> anArray.size
>> => 9
>> anArray.map {|e| e if e != 4}.compact
>> => [1, 2, 3, 5, 6, 7, 8, 9]
>> anArray.map {|e| e if e != 4}.compact.size
>> => 8
>>
>>
>> is ther a better way to do it... it it's enough ? (learning always how
>> to write better code..)
>
> Array#delete works also with no need for a block (i.e. you're only
> checking for equality)
>
> anArray = [1,2,3,4,5,6]
> p anArray.delete(4)
> p anArray.size

I checked
anArray.delete(0)
=> nil
then
anArray.delete(0).size
NoMethodError: undefined method `size' for nil:NilClass

as I tried to write it in one line....

thanks

joss



Josselin

6/3/2007 3:48:00 PM

0

On 2007-06-03 16:35:16 +0200, Tim Hunter <TimHunter@nc.rr.com> said:

> Josselin wrote:
>> I am trying to delete an element from an array if it exists and count
>> the number of item afterwards
>>
> How about using Array#delete_if?

that's exact§ly what I want... I read the API doc, I look into the
delete.. but why I stop before the delete_if ?????
thanks a lot ..

Joss

Todd Benson

6/3/2007 6:59:00 PM

0

On 6/3/07, Josselin <josselin@wanadoo.fr> wrote:
> On 2007-06-03 16:57:43 +0200, "Todd Benson" <caduceass@gmail.com> said:
>
> > On 6/3/07, Josselin <josselin@wanadoo.fr> wrote:
> >> I am trying to delete an element from an array if it exists and count
> >> the number of item afterwards
> >>
> >> I wrote
> >>
> >> anArray = [1,2,3,4,5,6,7,8,9]
> >> anArray.size
> >> => 9
> >> anArray.map {|e| e if e != 4}.compact
> >> => [1, 2, 3, 5, 6, 7, 8, 9]
> >> anArray.map {|e| e if e != 4}.compact.size
> >> => 8
> >>
> >>
> >> is ther a better way to do it... it it's enough ? (learning always how
> >> to write better code..)
> >
> > Array#delete works also with no need for a block (i.e. you're only
> > checking for equality)
> >
> > anArray = [1,2,3,4,5,6]
> > p anArray.delete(4)
> > p anArray.size
>
> I checked
> anArray.delete(0)
> => nil
> then
> anArray.delete(0).size
> NoMethodError: undefined method `size' for nil:NilClass
>
> as I tried to write it in one line....
>
> thanks
>
> joss

That's because Array#delete returns the deletion, not the Array
itself, so, if it has to be on one line, then:

(anArray.delete(4); a).size

or

anArray.delete_if {|i| i == 4}.size

or I prefer

anArray.reject! {|i| i == 4}.size

#delete, #delete_if, and #reject! all change the array while #reject does not