array.sort

Discussion in 'Ruby' started by Mark Ransom, Aug 29, 2007.

  1. Mark Ransom

    Mark Ransom Guest

    Hi,

    I'm a novice programmer who is just starting out in Ruby. I've been
    playing around with arrays and have run into a problem:

    This works:

    array = [3,2,1]
    puts array.sort

    =>123

    BUT this doesn't (error attached):

    nums = Array.new

    numplays = 5

    numplays.times do

    for values in 1..5
    ball = rand(56)
    redo if ball == 0 || nums.include?(ball)
    nums [values] = ball
    end
    puts nums.sort
    end

    Can anyone shed light on this newby?

    Attachments:
    http://www.ruby-forum.com/attachment/183/array_sort.JPG

    --
    Posted via http://www.ruby-forum.com/.
    Mark Ransom, Aug 29, 2007
    #1
    1. Advertising

  2. Alle mercoled=C3=AC 29 agosto 2007, Mark Ransom ha scritto:
    > Hi,
    >
    > I'm a novice programmer who is just starting out in Ruby. I've been
    > playing around with arrays and have run into a problem:
    >
    > This works:
    >
    > array =3D [3,2,1]
    > puts array.sort
    >
    > =3D>123
    >
    > BUT this doesn't (error attached):
    >
    > nums =3D Array.new
    >
    > numplays =3D 5
    >
    > numplays.times do
    >
    > for values in 1..5
    > ball =3D rand(56)
    > redo if ball =3D=3D 0 || nums.include?(ball)
    > nums [values] =3D ball
    > end
    > puts nums.sort
    > end
    >
    > Can anyone shed light on this newby?
    >
    > Attachments:
    > http://www.ruby-forum.com/attachment/183/array_sort.JPG


    Your array ends up having 6 elements, the first of which is nil. This happe=
    ns=20
    because your values variable starts from 1, while arrays indexes start at 0=
    =2E=20
    sort doesn't work on arrays which contain nil elements, because nil doesn't=
    =20
    have the <=3D> operator, which is used by sort to compare elements. To solv=
    e=20
    your problems, you should replace 1..5 with 0..4 (or use 5.times which, in =
    my=20
    opinion, is much clearer).

    By the way, you can avoid to check whether ball is 0 by replacing

    ball =3D rand(56)

    with

    ball =3D rand(55) + 1

    I hope this helps

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

  3. Mark Ransom

    Stephen Ball Guest

    On 8/29/07, Mark Ransom <> wrote:
    > Hi,
    >
    > I'm a novice programmer who is just starting out in Ruby. I've been
    > playing around with arrays and have run into a problem:
    >
    > This works:
    >
    > array = [3,2,1]
    > puts array.sort
    >
    > =>123
    >
    > BUT this doesn't (error attached):
    >
    > nums = Array.new
    >
    > numplays = 5
    >
    > numplays.times do
    >
    > for values in 1..5
    > ball = rand(56)
    > redo if ball == 0 || nums.include?(ball)
    > nums [values] = ball
    > end
    > puts nums.sort
    > end
    >
    > Can anyone shed light on this newby?
    >


    The problem is that you start your array index at 1, not 0.

    Orig: for values in 1..5
    Mod: for values in 0..4

    If you want to see what's happening in your code, add a 'puts
    nums.inspect' right after you assign the ball value to nums.

    -- Stephen
    Stephen Ball, Aug 29, 2007
    #3
  4. On Aug 29, 8:39 am, Mark Ransom <> wrote:
    > Hi,
    >
    > I'm a novice programmer who is just starting out in Ruby. I've been
    > playing around with arrays and have run into a problem:
    >
    > This works:
    >
    > array = [3,2,1]
    > puts array.sort
    >
    > =>123
    >
    > BUT this doesn't (error attached):
    >
    > nums = Array.new
    >
    > numplays = 5
    >
    > numplays.times do
    >
    > for values in 1..5
    > ball = rand(56)
    > redo if ball == 0 || nums.include?(ball)
    > nums [values] = ball
    > end
    > puts nums.sort
    > end
    >
    > Can anyone shed light on this newby?
    >
    > Attachments:http://www.ruby-forum.com/attachment/183/array_sort.JPG
    >
    > --
    > Posted viahttp://www.ruby-forum.com/.


    Ah, Stefano got to it before me, but here's a reworking of it anyway:

    numplays = 5
    numballs = 5

    numplays.times do
    nums = Array.new
    numballs.times do
    ball = rand(55) + 1
    redo if nums.include?(ball)
    nums.push ball
    end
    puts 'nums'
    puts nums.sort
    end


    --
    -yossef
    Yossef Mendelssohn, Aug 29, 2007
    #4
  5. Hi,

    On Wed, Aug 29, 2007 at 10:39:21PM +0900, Mark Ransom wrote:
    > Hi,
    >
    > I'm a novice programmer who is just starting out in Ruby. I've been
    > playing around with arrays and have run into a problem:
    >
    > This works:
    >
    > array = [3,2,1]
    > puts array.sort
    >
    > =>123
    >
    > BUT this doesn't (error attached):
    >
    > nums = Array.new
    >
    > numplays = 5
    >
    > numplays.times do
    >
    > for values in 1..5
    > ball = rand(56)
    > redo if ball == 0 || nums.include?(ball)
    > nums [values] = ball

    puts nums.join(',')
    > end
    > puts nums.sort
    > end
    >
    > Can anyone shed light on this newby?


    So what you are attempting to do is generate 25 random numbers between 1
    and 55, with no duplicates?

    > http://www.ruby-forum.com/attachment/183/array_sort.JPG


    The problem you are having is that when you get a number, you insert it
    into the array at slots 1,2,3,4 and 5. But your array is actually a 0
    based index. The first slot is nums[0]. That means when you do your
    sort you are sorting for instance [nil,2,32,1,44,26] which is an array
    of size 6, and nil doesn't compare with an integer via the <=> operator.

    The simpliest way to solve your issue is to change:

    for values in 1..5

    to

    5.times do |values|

    If you want to change it up even more and not have to 'redo' then you
    might change nums to a Hash and use its keys to hold the 'ball' values

    nums = {}
    while nums.size < 5 do
    ball = rand(55) + 1 # gives a value 1..55
    nums[ball] = ball # put ball into the hash
    end
    puts num.keys.sort.join(',')

    It appears the basic problem you are doing is to choose 5 distinct
    numbers from 1..55 N times. You may want to look at this ruby quiz for
    interesting items:

    http://www.rubyquiz.com/quiz39.html

    enjoy,

    -jeremy

    --
    ========================================================================
    Jeremy Hinegardner
    Jeremy Hinegardner, Aug 29, 2007
    #5
  6. Mark Ransom

    Mark Ransom Guest

    Stefano Crocco wrote:
    > Alle mercoledì 29 agosto 2007, Mark Ransom ha scritto:
    >> =>123
    >> ball = rand(56)
    >> redo if ball == 0 || nums.include?(ball)
    >> nums [values] = ball
    >> end
    >> puts nums.sort
    >> end
    >>
    >> Can anyone shed light on this newby?
    >>
    >> Attachments:
    >> http://www.ruby-forum.com/attachment/183/array_sort.JPG

    >
    > Your array ends up having 6 elements, the first of which is nil. This
    > happens
    > because your values variable starts from 1, while arrays indexes start
    > at 0.
    > sort doesn't work on arrays which contain nil elements, because nil
    > doesn't
    > have the <=> operator, which is used by sort to compare elements. To
    > solve
    > your problems, you should replace 1..5 with 0..4 (or use 5.times which,
    > in my
    > opinion, is much clearer).
    >
    > By the way, you can avoid to check whether ball is 0 by replacing
    >
    > ball = rand(56)
    >
    > with
    >
    > ball = rand(55) + 1
    >
    > I hope this helps
    >
    > Stefano


    Stefano,

    Thanks for the quick reply and help, now that you've pointed it out it
    seems so obvious. The one thing I could not find was a lot of
    information on the rand function so your help there is also greatly
    appreciated!

    Mark
    --
    Posted via http://www.ruby-forum.com/.
    Mark Ransom, Aug 29, 2007
    #6
  7. Mark Ransom wrote:
    > for values in 1..5
    > ball = rand(56)
    > redo if ball == 0 || nums.include?(ball)
    > nums [values] = ball
    > end


    nums=(1..55).sort_by {rand}.first(5)

    HTH,
    Sebastian
    --
    NP: Metallica - Creeping Death
    Jabber:
    ICQ: 205544826
    Sebastian Hungerecker, Aug 29, 2007
    #7
  8. Mark Ransom

    Mark Ransom Guest

    Thanks again, its invaluable to see the same actions performed using
    different code! I've been through many of the tutorials and 100 pages
    into "Beginning Ruby on Rails" and have not seen 'push' or sorting a
    hash using '.keys.sort.join(',') so again a great help!
    --
    Posted via http://www.ruby-forum.com/.
    Mark Ransom, Aug 29, 2007
    #8
  9. On 8/29/07, Mark Ransom <> wrote:
    > Thanks again, its invaluable to see the same actions performed using
    > different code! I've been through many of the tutorials and 100 pages
    > into "Beginning Ruby on Rails" and have not seen 'push' or sorting a
    > hash using '.keys.sort.join(',') so again a great help!
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >


    FYI, you can copy and paste from cmd.exe, you don't need to take
    screenshots. One way is click on the icon in the upper left hand
    corner, go do to edit, and then mark, now you can select the text you
    need, go back to the edit menu and hit copy.
    Logan Capaldo, Aug 29, 2007
    #9
    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. rkk
    Replies:
    9
    Views:
    801
    CBFalconer
    Sep 24, 2006
  2. a

    sort array of array

    a, Nov 22, 2007, in forum: C Programming
    Replies:
    0
    Views:
    296
  3. Navin
    Replies:
    1
    Views:
    668
    Ken Schaefer
    Sep 9, 2003
  4. GIMME
    Replies:
    5
    Views:
    177
    Thomas 'PointedEars' Lahn
    Jul 26, 2004
  5. Domenico Discepola

    multi-field array sort using Sort::Fields method

    Domenico Discepola, Apr 27, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    290
    Uri Guttman
    Apr 28, 2004
Loading...

Share This Page