[ARRAYS] Bidimensional Arrays? (opened)

Discussion in 'Ruby' started by Flaab Mrlinux, Nov 14, 2006.

  1. Hi there!

    I'm just new at ruby and I have a weird issue probably really dumb but i
    just haven't been able to figure it out.

    Using arrays in C or whatever i could define an array using to indexing
    numbers, in order to simulate a chess board or whatever...

    board = array[8,8]

    And then store info in that array like this

    board[1,1] = whatever.

    I just can't get that to work in ruby! Why? How can i do it?

    Thx

    --
    Posted via http://www.ruby-forum.com/.
     
    Flaab Mrlinux, Nov 14, 2006
    #1
    1. Advertising

  2. Flaab Mrlinux

    Tim Pease Guest

    On 11/14/06, Flaab Mrlinux <> wrote:
    > Hi there!
    >
    > I'm just new at ruby and I have a weird issue probably really dumb but i
    > just haven't been able to figure it out.
    >
    > Using arrays in C or whatever i could define an array using to indexing
    > numbers, in order to simulate a chess board or whatever...
    >
    > board = array[8,8]
    >
    > And then store info in that array like this
    >
    > board[1,1] = whatever.
    >
    > I just can't get that to work in ruby! Why? How can i do it?
    >


    The ruby Array class is always a one dimensional array. You can create
    an array of arrays to get two dimensional behavior.

    ary = Array.new(3) {|idx| Array.new(3)}
    ary[0][0] = 1

    To make the indexing a little more clear

    tmp =ary[0] # give me the row at index 0
    tmp[0] = 1 # set the value at column 0 of row 0 to 1 (since
    tmp is really row 0)

    But usually you can just glom all that together like so ...

    ary[0][0] = 1
    ary[0][1] = 2
    ary[0][2] = 3


    I hope this answers your question.

    TwP
     
    Tim Pease, Nov 15, 2006
    #2
    1. Advertising

  3. Flaab Mrlinux wrote:
    > Hi there!
    >
    > I'm just new at ruby and I have a weird issue probably really dumb but i
    > just haven't been able to figure it out.
    >
    > Using arrays in C or whatever i could define an array using to indexing
    > numbers, in order to simulate a chess board or whatever...
    >
    > board = array[8,8]
    >
    > And then store info in that array like this
    >
    > board[1,1] = whatever.
    >
    > I just can't get that to work in ruby! Why? How can i do it?
    >
    > Thx
    >
    >

    The easiest way to define a multidimensional array in Ruby is to define
    an array of arrays:

    board = Array.new(8) { Array.new(8) }

    Run "ri Array.new" to read about how to create and initialize arrays.
     
    Timothy Hunter, Nov 15, 2006
    #3
    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. Joona I Palaste

    Re: bidimensional array of char (function)

    Joona I Palaste, Jul 17, 2003, in forum: C Programming
    Replies:
    0
    Views:
    453
    Joona I Palaste
    Jul 17, 2003
  2. Barry Schwarz

    Re: bidimensional array of char (function)

    Barry Schwarz, Jul 18, 2003, in forum: C Programming
    Replies:
    0
    Views:
    493
    Barry Schwarz
    Jul 18, 2003
  3. Pedro Pinto

    Linked list with bidimensional array

    Pedro Pinto, Oct 31, 2006, in forum: C Programming
    Replies:
    11
    Views:
    578
    Pedro Pinto
    Oct 31, 2006
  4. nick
    Replies:
    0
    Views:
    373
  5. Santiago  Romero
    Replies:
    9
    Views:
    259
    Diez B. Roggisch
    Jan 11, 2008
Loading...

Share This Page