classification

Discussion in 'Ruby' started by Josselin, Jan 29, 2007.

  1. Josselin

    Josselin Guest

    I have to classify a bunch of products into 4 boxes, according to the
    value of one of their attribute (product.code , an integer between 0
    and 4)

    I wrote that but it's seems wrong :

    box[0] = box[1] = box[2] = box[3] = []

    for product in products
    tag = Tag.new(product.title, product.price)
    box[ product.code ] << tag
    end

    then I'll reuse each box[] for further processing...

    what's wrong ?

    tfyl

    joss
     
    Josselin, Jan 29, 2007
    #1
    1. Advertising

  2. Alle luned=EC 29 gennaio 2007, Josselin ha scritto:
    > I have to classify a bunch of products into 4 boxes, according to the
    > value of one of their attribute (product.code , an integer between 0
    > and 4)
    >
    > I wrote that but it's seems wrong :
    >
    > box[0] =3D box[1] =3D box[2] =3D box[3] =3D []
    >
    > for product in products
    > tag =3D Tag.new(product.title, product.price)
    > box[ product.code=A0] << tag
    > end
    >
    > then I'll reuse each box[] for further processing...
    >
    > what's wrong ?
    >
    > tfyl
    >
    > joss


    The problem is that writing something like

    a =3D b =3D c =3D []

    will store the same array in all the variables. If you use the object_id=20
    methods on the four elements of box, you'll see that the returned value is=
    =20
    the same. This means that modifiying, for instance box[0] will also modify=
    =20
    box[1], box[2] and box[3], because they all contain the same object. You=20
    should replace the line

    box[0] =3D box[1] =3D box[2] =3D box[3] =3D []

    with, for example,

    box=3DArray.new(4){[]}

    This will create a new Array with four elements, each of which is a differe=
    nt=20
    Array. Note that writing box=3DArray.new(4,[]) will produce the same (wrong=
    )=20
    result as your code.

    I hope this helps

    Stefano
     
    Stefano Crocco, Jan 29, 2007
    #2
    1. Advertising

  3. Josselin

    Josselin Guest

    On 2007-01-29 18:18:31 +0100, Stefano Crocco <> said:

    > Alle lunedì 29 gennaio 2007, Josselin ha scritto:
    >> I have to classify a bunch of products into 4 boxes, according to the
    >> value of one of their attribute (product.code , an integer between 0
    >> and 4)
    >>
    >> I wrote that but it's seems wrong :
    >>
    >> box[0] = box[1] = box[2] = box[3] = []
    >>
    >> for product in products
    >> tag = Tag.new(product.title, product.price)
    >> box[ product.code ] << tag
    >> end
    >>
    >> then I'll reuse each box[] for further processing...
    >>
    >> what's wrong ?
    >>
    >> tfyl
    >>
    >> joss

    >
    > The problem is that writing something like
    >
    > a = b = c = []
    >
    > will store the same array in all the variables. If you use the object_id
    > methods on the four elements of box, you'll see that the returned value is
    >
    > the same. This means that modifiying, for instance box[0] will also modify
    >
    > box[1], box[2] and box[3], because they all contain the same object. You
    > should replace the line
    >
    > box[0] = box[1] = box[2] = box[3] = []
    >
    > with, for example,
    >
    > box=Array.new(4){[]}
    >
    > This will create a new Array with four elements, each of which is a differe
    > nt
    > Array. Note that writing box=Array.new(4,[]) will produce the same (wrong
    > )
    > result as your code.
    >
    > I hope this helps
    >
    > Stefano


    thansk a lot .. it's so DRY...

    btw : I tried also

    box = []
    0.upto(4) do |i|
    box = []
    end
     
    Josselin, Jan 29, 2007
    #3
  4. On 29.01.2007 19:20, Josselin wrote:
    > On 2007-01-29 18:18:31 +0100, Stefano Crocco <>
    > said:
    >
    >> Alle lunedì 29 gennaio 2007, Josselin ha scritto:
    >>> I have to classify a bunch of products into 4 boxes, according to the
    >>> value of one of their attribute (product.code , an integer between 0
    >>> and 4)
    >>>
    >>> I wrote that but it's seems wrong :
    >>>
    >>> box[0] = box[1] = box[2] = box[3] = []
    >>>
    >>> for product in products
    >>> tag = Tag.new(product.title, product.price)
    >>> box[ product.code ] << tag
    >>> end
    >>>
    >>> then I'll reuse each box[] for further processing...
    >>>
    >>> what's wrong ?
    >>>
    >>> tfyl
    >>>
    >>> joss

    >>
    >> The problem is that writing something like
    >>
    >> a = b = c = []
    >>
    >> will store the same array in all the variables. If you use the object_id
    >> methods on the four elements of box, you'll see that the returned
    >> value is
    >>
    >> the same. This means that modifiying, for instance box[0] will also
    >> modify
    >>
    >> box[1], box[2] and box[3], because they all contain the same object. You
    >> should replace the line
    >>
    >> box[0] = box[1] = box[2] = box[3] = []
    >>
    >> with, for example,
    >>
    >> box=Array.new(4){[]}
    >>
    >> This will create a new Array with four elements, each of which is a
    >> differe
    >> nt
    >> Array. Note that writing box=Array.new(4,[]) will produce the same (wrong
    >> )
    >> result as your code.
    >>
    >> I hope this helps
    >>
    >> Stefano

    >
    > thansk a lot .. it's so DRY...
    >
    > btw : I tried also
    >
    > box = []
    > 0.upto(4) do |i|
    > box = []
    > end
    >

    Much more convenient is this:

    box = Hash.new {|h,k| h[k] = []}

    Now you can just do

    for product in products
    tag = Tag.new(product.title, product.price)
    box[ product.code ] << tag
    end

    Kind regards

    robert
     
    Robert Klemme, Jan 29, 2007
    #4
    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. Corn Holio

    Text Classification - Spam Filter

    Corn Holio, Jan 3, 2004, in forum: Java
    Replies:
    6
    Views:
    3,585
    Pavel Tonkov
    Jan 4, 2004
  2. Replies:
    1
    Views:
    600
    Paul Lutus
    Sep 8, 2004
  3. Stefan Ram
    Replies:
    4
    Views:
    484
    Chris Uppal
    Feb 1, 2006
  4. Internet Citizen

    Character classification: novice question

    Internet Citizen, May 14, 2004, in forum: C++
    Replies:
    2
    Views:
    479
    Internet Citizen
    May 14, 2004
  5. Robin Becker

    spam classification breaker

    Robin Becker, Feb 5, 2004, in forum: Python
    Replies:
    4
    Views:
    337
    Robin Becker
    Feb 6, 2004
Loading...

Share This Page