delete in Array (if exist) and count

Discussion in 'Ruby' started by Josselin, Jun 3, 2007.

  1. Josselin

    Josselin Guest

    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
    Josselin, Jun 3, 2007
    #1
    1. Advertising

  2. Josselin

    Tim Hunter Guest

    Tim Hunter, Jun 3, 2007
    #2
    1. Advertising

  3. Josselin

    Todd Benson Guest

    On 6/3/07, Josselin <> 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
    Todd Benson, Jun 3, 2007
    #3
  4. Josselin

    Josselin Guest

    On 2007-06-03 16:57:43 +0200, "Todd Benson" <> said:

    > On 6/3/07, Josselin <> 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, Jun 3, 2007
    #4
  5. Josselin

    Josselin Guest

    On 2007-06-03 16:35:16 +0200, Tim Hunter <> 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
    Josselin, Jun 3, 2007
    #5
  6. Josselin

    Todd Benson Guest

    On 6/3/07, Josselin <> wrote:
    > On 2007-06-03 16:57:43 +0200, "Todd Benson" <> said:
    >
    > > On 6/3/07, Josselin <> 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
    Todd Benson, Jun 3, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Y.S.
    Replies:
    3
    Views:
    1,013
    strajan
    Sep 17, 2003
  2. LT
    Replies:
    7
    Views:
    2,094
    Phlip
    Jul 25, 2004
  3. efelnavarro09
    Replies:
    2
    Views:
    934
    efelnavarro09
    Jan 26, 2011
  4. Jean-Sébastien

    count : array to array of hashes

    Jean-Sébastien, Aug 1, 2007, in forum: Ruby
    Replies:
    2
    Views:
    125
    Stefano Crocco
    Aug 1, 2007
  5. Markus Mohr
    Replies:
    7
    Views:
    248
    Thomas 'PointedEars' Lahn
    Nov 28, 2003
Loading...

Share This Page