Array within array question

Discussion in 'Ruby' started by Josh Rowe, Jan 20, 2011.

  1. Josh Rowe

    Josh Rowe Guest

    Hello everyone, this is my first post on this forum and I'm quite new to
    ruby. My dilemma is that I've got an array which holds a whole lot of
    arrays inside it and I want to print a element of one of the arrays
    within the overall array. Got it? Here's an example

    #!/usr/bin/env ruby
    card1 = ["Ninja Monster", "monster", 1400]
    card2 = ["Ninja Mage", "monster", 1600]
    cdname = 0
    deck = [card1,card2]
    puts deck[0[cdname]]

    Does anyone(I'm sure most of you) know how to get this to work with a
    minimal amount of extra code.

    Regards Joshua

    --
    Posted via http://www.ruby-forum.com/.
     
    Josh Rowe, Jan 20, 2011
    #1
    1. Advertising

  2. On Thursday 20 January 2011 18:34:26 Josh Rowe wrote:
    > Hello everyone, this is my first post on this forum and I'm quite new to
    > ruby. My dilemma is that I've got an array which holds a whole lot of
    > arrays inside it and I want to print a element of one of the arrays
    > within the overall array. Got it? Here's an example
    >
    > #!/usr/bin/env ruby
    > card1 = ["Ninja Monster", "monster", 1400]
    > card2 = ["Ninja Mage", "monster", 1600]
    > cdname = 0
    > deck = [card1,card2]
    > puts deck[0[cdname]]
    >
    > Does anyone(I'm sure most of you) know how to get this to work with a
    > minimal amount of extra code.
    >
    > Regards Joshua


    puts deck[0][cdname]

    The [] method of class array (Array#[]), which is what you actually call when
    writing an_array[3] returns the object with that index in the array. In your
    case, deck[0] returns the element with index 0, that is the array card1. To
    retrieve the name of the card, you then access the element with index cdname
    of this array, again using []. To be more explicit, you can split the above
    line in two using an intermediate variable:

    card = deck[0]
    puts card[cdname]

    However I think that using arrays to represent cards isn't the best way to go.
    Arrays usually represent lists of things (for example, it's correct to use
    them for a deck of cards). The contents of your cards, however, aren't lists,
    since each item has a different meaning.

    A better way to represent cards would be to use hashes:

    card1 = {:name => "Ninja Monster", :type => "monster", :number => 1400}
    card2 = {:name => "Ninja Mage", :type => "monster", :number => 1600}
    deck = [card1, card2]
    puts deck[0][:name]

    I hope this helps

    Stefano
     
    Stefano Crocco, Jan 20, 2011
    #2
    1. Advertising

  3. Josh Rowe

    Josh Rowe Guest

    Stefano Crocco wrote in post #976226:
    > On Thursday 20 January 2011 18:34:26 Josh Rowe wrote:
    >> puts deck[0[cdname]]
    >>
    >> Does anyone(I'm sure most of you) know how to get this to work with a
    >> minimal amount of extra code.
    >>
    >> Regards Joshua

    >
    > puts deck[0][cdname]
    >
    > The [] method of class array (Array#[]), which is what you actually call
    > when
    > writing an_array[3] returns the object with that index in the array. In
    > your
    > case, deck[0] returns the element with index 0, that is the array card1.
    > To
    > retrieve the name of the card, you then access the element with index
    > cdname
    > of this array, again using []. To be more explicit, you can split the
    > above
    > line in two using an intermediate variable:
    >
    > card = deck[0]
    > puts card[cdname]
    >
    > However I think that using arrays to represent cards isn't the best way
    > to go.
    > Arrays usually represent lists of things (for example, it's correct to
    > use
    > them for a deck of cards). The contents of your cards, however, aren't
    > lists,
    > since each item has a different meaning.
    >
    > A better way to represent cards would be to use hashes:
    >
    > card1 = {:name => "Ninja Monster", :type => "monster", :number => 1400}
    > card2 = {:name => "Ninja Mage", :type => "monster", :number => 1600}
    > deck = [card1, card2]
    > puts deck[0][:name]
    >
    > I hope this helps
    >
    > Stefano


    Thank you for that lightning quick response. I've just printed out some
    pages on Hashes from an ebook and from what I've seen your advice seems
    to be a good way to go. This game is only in early development and it's
    good to make changes like this near the start. I will start changing my
    other code to use hashes now. It's good to see such a great community
    like this forum which has lots of people who readily give up there
    expertise to help others.

    Thanks again Stefano and have a good year in ruby programming!

    --
    Posted via http://www.ruby-forum.com/.
     
    Josh Rowe, Jan 20, 2011
    #3
  4. On Thu, Jan 20, 2011 at 10:34 AM, Josh Rowe <> wrote:
    > Hello everyone, this is my first post on this forum and I'm quite new to
    > ruby. My dilemma is that I've got an array which holds a whole lot of
    > arrays inside it and I want to print a element of one of the arrays
    > within the overall array. Got it? Here's an example
    >
    > #!/usr/bin/env ruby
    > card1 = ["Ninja Monster", "monster", 1400]
    > card2 = ["Ninja Mage", "monster", 1600]
    > cdname = 0
    > deck = [card1,card2]
    > puts deck[0[cdname]]


    You're almost there:

    puts deck[0][cdname]

    Nesting does not apply to indexes. Instead you need to unnest step by
    step. Hence the sequence of [idx] operations.

    > Does anyone(I'm sure most of you) know how to get this to work with a
    > minimal amount of extra code.


    See above.

    Cheers

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Jan 20, 2011
    #4
  5. On Thu, Jan 20, 2011 at 11:02 AM, Stefano Crocco
    <> wrote:
    > On Thursday 20 January 2011 18:34:26 Josh Rowe wrote:
    >> Hello everyone, this is my first post on this forum and I'm quite new to
    >> ruby. My dilemma is that I've got an array which holds a whole lot of
    >> arrays inside it and I want to print a element of one of the arrays
    >> within the overall array. Got it? Here's an example
    >>
    >> #!/usr/bin/env ruby
    >> card1 = ["Ninja Monster", "monster", 1400]
    >> card2 = ["Ninja Mage", "monster", 1600]
    >> cdname = 0
    >> deck = [card1,card2]
    >> puts deck[0[cdname]]
    >>
    >> Does anyone(I'm sure most of you) know how to get this to work with a
    >> minimal amount of extra code.
    >>
    >> Regards Joshua

    >
    > puts deck[0][cdname]
    >
    > The [] method of class array (Array#[]), which is what you actually call when
    > writing an_array[3] returns the object with that index in the array. In your
    > case, deck[0] returns the element with index 0, that is the array card1. To
    > retrieve the name of the card, you then access the element with index cdname
    > of this array, again using []. To be more explicit, you can split the above
    > line in two using an intermediate variable:
    >
    > card = deck[0]
    > puts card[cdname]
    >
    > However I think that using arrays to represent cards isn't the best way to go.
    > Arrays usually represent lists of things (for example, it's correct to use
    > them for a deck of cards). The contents of your cards, however, aren't lists,
    > since each item has a different meaning.
    >
    > A better way to represent cards would be to use hashes:
    >
    > card1 = {:name => "Ninja Monster", :type => "monster", :number => 1400}
    > card2 = {:name => "Ninja Mage", :type => "monster", :number => 1600}
    > deck = [card1, card2]
    > puts deck[0][:name]


    Or even

    Card = Struct.new :name, :type, :number
    deck = [
    Card["Ninja Monster", :monster, 1400],
    Card["Ninja Mage", :monster, 1600],
    ]

    puts deck[0].name

    Note that beside using Struct to quickly create a proper class I also
    used symbols as type names since you will most likely have only few of
    those which repeat often and in this case symbols are more efficient
    than strings.

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Jan 20, 2011
    #5
    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. KatB
    Replies:
    0
    Views:
    455
  2. mark4asp
    Replies:
    2
    Views:
    2,242
    Harry Haller
    Nov 7, 2006
  3. Elmo Watson
    Replies:
    1
    Views:
    606
    Eliyahu Goldin
    May 8, 2007
  4. Ultrus
    Replies:
    3
    Views:
    390
    Stefan Behnel
    Jul 9, 2007
  5. Abby Lee
    Replies:
    5
    Views:
    443
    Abby Lee
    Aug 2, 2004
Loading...

Share This Page