Jump to content

How do I fill a 2 dimensional array on declaration?

Recommended Posts


Yes the syntax is correct;

Local $Array[10][10] = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]

and works, but only gives the first 2 rows; messy for large number of rows.

?Consider writing each "row" into a 1D Array which has its own dimension; [so use ArrayCreate or StringSplit, but watch base 0 or 1.] [This handles the dimensions for you, even if the rows are different lengths; you only need to declare the number of Arrays to be nested in the Array of Arrays....]

See my Array2D post from signature;

#include <Array2D.au3>

Dim $ar1_Array_Arrays[4]; base 0 for Array of arrays; you can add empty zero row later if needed

$ar1_Array_Arrays[0] = _ArrayCreate('animal names', 'aardvark', 'badger','beaver','cat') ; this is always strings starting at zero; base "0"

$ar1_Array_Arrays[1] = _Stringsplit_B0('plant names|Tree|flower|grass|petunia|Tree|flower|grass|petunia'); else "StringSplit" has strings starting at 1; base "1"

$ar1_Array_Arrays[2] = _Stringsplit_B0('people names|Anne|Joanne|Joan|Cherry')

$ar1_Array_Arrays[3] = _Stringsplit_B0('car names|holden|ford|volkswagon|mitsubishi')

$ar2_Array=_Array2DCreateFromArray($ar1_Array_Arrays )

_ArrayViewText($ar2_Array, 'Display',0,0,0,0)

Best, Randall Edited by randallc
Link to comment
Share on other sites

Lemme describe what I'm trying to do and maybe it will shed some light on this problem.

I have a 7x7 (2D array) square and in each box (element) is a number. I need to be able to check each element (out of 49) and compare it against another element. There are no strings to deal with and using an on-the-fly array creation might not be the best case for this. Also, when the script loads, some of the elements alraedy have numbers in them. Here is how it needs to load:

Row 1: [0][0][0][0][6][0][0]

Row 2: [0][4][0][0][0][0][0]

Row 3: [0][0][0][0][0][3][0]

Row 4: [0][0][2][5][0][0][0]

Row 5: [0][7][0][0][0][0][0]

Row 6: [0][0][0][1][2][0][0]

Row 7: [0][0][0][0][0][0][0]

Total Elements: 49, Total blank elements (zeros): 41

Now, from there I have to replace the zero's with a number from 1 to 7 as long as:

1.) That number is not already on that row

2.) That number is not already on that column

3.) That number is not already in the two diagonal lines (corner to corner) connected to that number. :EDIT:

----For example, Row 1:Element 1 cannot be a 4 or a 6 and Row 1:Element 7 cannot be a 6 or a 5

I came across this number puzzle from my girlfriend and she's stumped. I figured it out within a few minutes and decided to try and write a script to figure it out (and to see if there is more than one answer). I'm not asking for someone to code it for me (no fun in that) just to get started. I've never been very good with multi-dimensional arrays throughout my programming career so I figure the best way to deal with that is to force myself to use them.



Edited by CodeMaster Rapture
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...