Pokémon Essentials All questions and discussion about Pokémon Essentials, the Pokémon game kit for RPG Maker XP, go in here. Also contains links to the latest downloads and the Essentials Wiki.

Reply
 
Thread Tools
  #1    
Old 2 Weeks Ago (6:27 PM).
Ezki11 Ezki11 is offline
     
    Join Date: Feb 2017
    Location: California
    Age: 19
    Gender: Male
    Nature: Careful
    Posts: 15
    Hello, I've been having trouble trying to push variables in 2 dimensional arrays. I don't even know if this is possible.

    The first thing I do is initialize the array like so:
    Code:
    array2=Array.new(3){Array.new}
    Next, I have two other variables, for example:
    Code:
    var1=3
    var2=rand(10)
    So what I'm trying to do is to push the array that is inside the array.
    Code:
    for x in 0...var1
       for i in 0...var2
          array2[x].push[i]=rand(50)
       end
    end
    However, this isn't working. Can you use the push function in 2 dimensional arrays? Or is the problem elsewhere?
    Reply With Quote

    Relevant Advertising!

      #2    
    Old 2 Weeks Ago (10:00 PM).
    mej71's Avatar
    mej71 mej71 is offline
    • Moderator
     
    Join Date: Mar 2014
    Location: Omnipresence
    Gender: Male
    Posts: 1,207
    push is a method to act on an array, you've got your syntax wrong

    Code:
    for x in 0...var1
       for i in 0...var2
          array2[x][i].push(rand(50))
       end
    end
    However, this is still wrong. You've only got a two level array here
    (looks like)
    Code:
    [
        [1,2,3,4,5],
        [1,2,3,4,7],
        [1,2,3,4,5]
    ]
    So your original code is also trying to use the push method on a member of the sub-array, not the array itself

    Code:
    for x in 0...array2.size  #var1 is uneccessary, since array2's size may change
        var2.times {array2[x].push(rand(50)) }
    end
    Reply With Quote
      #3    
    Old 2 Weeks Ago (11:06 PM).
    Ezki11 Ezki11 is offline
       
      Join Date: Feb 2017
      Location: California
      Age: 19
      Gender: Male
      Nature: Careful
      Posts: 15
      Quote:
      Originally Posted by mej71 View Post
      push is a method to act on an array, you've got your syntax wrong

      Code:
      for x in 0...var1
         for i in 0...var2
            array2[x][i].push(rand(50))
         end
      end
      However, this is still wrong. You've only got a two level array here
      (looks like)
      Code:
      [
          [1,2,3,4,5],
          [1,2,3,4,7],
          [1,2,3,4,5]
      ]
      So your original code is also trying to use the push method on a member of the sub-array, not the array itself

      Code:
      for x in 0...array2.size  #var1 is uneccessary, since array2's size may change
          var2.times {array2[x].push(rand(50)) }
      end
      Ah, I see. I've been running a few tests on some Ruby editors and yeah, I see that now. Thanks for clearing that up!
      Reply With Quote
        #4    
      Old 2 Weeks Ago (2:58 AM).
      塗 ゆり's Avatar
      塗 ゆり 塗 ゆり is offline
      Scripter / RGSS Modder
         
        Join Date: Apr 2011
        Location: France
        Age: 23
        Gender: Other
        Posts: 60
        You can also init your array like this :
        Code:
        array2 = Array.new(3) { Array.new(rand(10)) { rand(50) } }
        PS : You can get the current index of the item while initializing the array like this :
        Code:
        array2 = Array.new(3) { |index_x| Array.new(rand(10)) { |index_y| rand(50) } }
        (If you want to put something else than random numbers)
        __________________
        Nuri Yuri
        Pokémon Workshop | Facebook Page
        Reply With Quote
        Reply
        Quick Reply

        Sponsored Links
        Thread Tools

        Posting Rules
        You may not post new threads
        You may not post replies
        You may not post attachments
        You may not edit your posts

        BB code is On
        Smilies are On
        [IMG] code is On
        HTML code is Off

        Forum Jump


        All times are GMT -8. The time now is 8:02 PM.