Two dimention arrays and accessing syslog.

Discussion in 'Ruby' started by Sean Murphy, Jan 14, 2008.

  1. Sean Murphy

    Sean Murphy Guest

    All,

    A newby question, only been using Ruby for about 2 days.

    Is there a better way of creating a two dimention array, than the code used
    below

    def create_world x, y
    row = Array.new

    y.times {
    |j|
    col = Array.new
    x.times {
    |i|

    map_type = rand(2)
    col = map_type
    }
    row[j] = col
    }

    return row
    end

    world = create_world(gets.to_i, gets.to_i)

    Also, what is the best method of accessing the syslog file within Linux? I
    could open the file for reading, but I want to make sure there isn't a
    better method first.

    Regards
    Sean Murphy
    Skype: smurf20005

    Life is a challenge, treat it that way.
     
    Sean Murphy, Jan 14, 2008
    #1
    1. Advertising

  2. Alle luned=EC 14 gennaio 2008, Sean Murphy ha scritto:
    > All,
    >
    > A newby question, only been using Ruby for about 2 days.
    >
    > Is there a better way of creating a two dimention array, than the code us=

    ed
    > below
    >
    > def create_world x, y
    > =A0 row =3D Array.new
    >
    > =A0 y.times {
    > =A0 =A0|j|
    > =A0 =A0col =3D Array.new
    > =A0 =A0x.times {
    > =A0 =A0 |i|
    >
    > =A0 =A0 =A0 map_type =3D rand(2)
    > =A0 =A0 =A0 col =3D map_type
    > =A0 =A0 }
    > =A0 =A0 row[j] =3D col
    > =A0 }
    >
    > =A0 return row
    > end
    >
    > world =3D create_world(gets.to_i, gets.to_i)


    This should work:

    def make_2d_array n_row, n_col
    Array.new(n_row) do
    Array.new(n_col){ rand(2) }
    end
    end

    make_2d_array(gets.to_i, gets.to_i)

    The block form of Array.new creates an array of the size passed as argument=
    ,=20
    then calls the block for each index of the array and stores the value=20
    returned by the block in the corresponding element of the array (the block=
    =20
    can take one parameter, the index of the element, which wasn't necessary in=
    =20
    this case). See the ri documentation for Array.new for more information.

    I hope this helps

    Stefano
     
    Stefano Crocco, Jan 14, 2008
    #2
    1. Advertising

  3. Sean Murphy

    Sean Murphy Guest

    Hi,

    thanks for that, that was really cool code.

    Regards
    Sean Murphy
    Skype: smurf20005

    Life is a challenge, treat it that way.
    ----- Original Message -----
    From: "Stefano Crocco" <>
    To: "ruby-talk ML" <>
    Sent: Monday, January 14, 2008 7:40 PM
    Subject: Re: Two dimention arrays and accessing syslog.


    Alle lunedì 14 gennaio 2008, Sean Murphy ha scritto:
    > All,
    >
    > A newby question, only been using Ruby for about 2 days.
    >
    > Is there a better way of creating a two dimention array, than the code
    > used
    > below
    >
    > def create_world x, y
    > row = Array.new
    >
    > y.times {
    > |j|
    > col = Array.new
    > x.times {
    > |i|
    >
    > map_type = rand(2)
    > col = map_type
    > }
    > row[j] = col
    > }
    >
    > return row
    > end
    >
    > world = create_world(gets.to_i, gets.to_i)


    This should work:

    def make_2d_array n_row, n_col
    Array.new(n_row) do
    Array.new(n_col){ rand(2) }
    end
    end

    make_2d_array(gets.to_i, gets.to_i)

    The block form of Array.new creates an array of the size passed as argument,
    then calls the block for each index of the array and stores the value
    returned by the block in the corresponding element of the array (the block
    can take one parameter, the index of the element, which wasn't necessary in
    this case). See the ri documentation for Array.new for more information.

    I hope this helps

    Stefano
     
    Sean Murphy, Jan 14, 2008
    #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. seamoon
    Replies:
    3
    Views:
    466
    red floyd
    Mar 8, 2005
  2. Replies:
    2
    Views:
    6,208
    Chris Uppal
    Jun 20, 2006
  3. Slim

    Return 2 dimention array from com

    Slim, May 28, 2004, in forum: ASP General
    Replies:
    3
    Views:
    109
    Bob Barrows [MVP]
    May 28, 2004
  4. leroy isaac
    Replies:
    1
    Views:
    523
    James Willmore
    Oct 29, 2004
  5. Bad Dog
    Replies:
    0
    Views:
    257
    Bad Dog
    Aug 9, 2007
Loading...

Share This Page