Two-dimensional arrays

Can someone clearly explain two-dimensional arrays for me?

Here’s the problem. I have a set of, let’s say 5, records with two important fields, A and B. I want to pick up the values in these two fields for each of the five records – A1 through A5, B1 through B5. Then loop through a much larger set of the same records, looking for records with the same value as in A1 and setting field B to the value in B1. Continue with A2 through A5.

In effect I’m making all records have the same settings as the original set of 5. Field A is being used for finding matches, only field B is being changed. I believe this calls for a two-dimensional array. Does anyone have experience with this sort of thing and give me some pointers?

Thanks.

As in your other thread: "Double" arrays - Classic Servoy - Servoy Community

That’s about as clear as it can be.

bcusick:
As in your other thread: "Double" arrays - Classic Servoy - Servoy Community

That’s about as clear as it can be.

Sorry, Bob, it’s not. With respect, that was a half answer. How do I get the required fields into and out of the array? That specific issue has not been dealt with. My various experiments with the syntax have all failed.

I’ve been pursuing this issue for several weeks in various topics AND with Servoy Support AND on in JavaScript forums. I’ve yet to get a clear and complete answer anywhere.

Becoming very frustrated. Assistance appreciated.

Morley:
Sorry, Bob, it’s not. With respect, that was a half answer. How do I get the required fields into and out of the array? That specific issue has not been dealt with. My various experiments with the syntax have all failed.

Adding to an array:
myArray[0][0] = ‘my value 1’;
myArray[0][1] = ‘my value 2’;

retrieving values from an array:
var myValue1 = myArray[0][0];
var myValue2 = myArray[0][1];

It’s pretty straightforward.

the basic idea is this for example:

anArray = new Array(4)
This will place an array at position i of anArray. Thus, you have a multidimension array here that you address for example like
anArray*[2]*
With this you get the third index position of the Array created above on position of anArray.
As stated above, a two dimensional Array (from the JavaScript reference) is this:
*_</em> <em><em>*a = new Array(4) // a is an Array with a fixed length for (i=0; i < 4; i++) { // loop through the index a[i] = new Array(4) // position i gets an Array for (j=0; j < 4; j++) { // loop through 2nd Array a[i][j] = "["+i+","+j+"]" // place Strings in position i,j } }*</em></em> <em>_*
Is this the kind of explanation you are looking for?

Thanks for both responses. Will see if they do the trick.

Hi Morley,

There’s also loads of info about javascript on the internet.
try for instance google search on “2 dimensional arrays javascript”

Sorry to be tiresome but I’m still not “getting” it. I’ve spent dozens of hours on this, so some handholding would be greatly appreciated.

To summarize, I have a set of records with two key fields. I need to capture the current settings of these fields so I can later use the same table to impose the setting of the second field in each case where the first field occurs. The first field is text, the second is integer.

The two fields in the first four records read like this:

L1C1, 1
L1C2, 1
L1C4,
L1C1L2C1, 1

Several weeks ago Maarten gave me the following code for a two-dimensional array.

var a = new Array(4)
for (i=0; i < 4; i++)
{
   a[i] = new Array(4)
   for (j=0; j < 4; j++)
   {
      a[i][j] = "["+i+","+j+"]"
   }
}

I’ve tried several variations to insert my two fields into this mill. Here’s my latest attempt (although I really feel I’m just flailing about). I’m missing something fundamental.

controller.setSelectedIndex(1);
var c = controller.getSelectedIndex();
var a = new Array(4); 
for (i=0; i < 4; i++)
{
	controller.setSelectedIndex(c++);
 	a[i] = new Array(); 
	for (j=0; j < 1; j++)
 	{
		a[i][j] = "[" + catkey + "," + tick + "]";
   	}
}
application.output('a = ' + a);

The above code produces this array:

a = [L1C1,1],[L1C2,1],[L1C4,],[L1C1L2C1,1]

This is the first variation that has produced something recognizable, although I’m not sure its form is the most useful for my purpose. See what I mean about being uncertain about what I’m doing?

Generally I can figure this stuff out after one or two prods. But not this time. I’ve reviewed various online JavaScript tutorials and am none the wiser. Sorry, but I’m currently too thick to get what’s going on here. The routine is very significant to my solution so I’m urgent to get this enigma resolved. Thanks.

Morley, what do you want to do with this:

L1C1, 1
L1C2, 1
L1C4,
L1C1L2C1, 1

You need to better explain your case. If you want to store those values in an Array, you can do something like

var anArray = new Array();
anArray[0] = new Array('L1C1', 1); // put an Array at position 0 of anArray
anArray[1] = ['L1C2', 1]; // does the same
anArray[2] = ['L1C4']; // puts an Array of length 1 at that position; if you don't want that, you have to do ['L1C4', null]
anArray[3] = ['L1C1L2C1, 1];

Of course you can create that array using a loop through your records, too:

var a = new Array()
for (i = 1; i <= controller.getMaxRecordIndex(); i++)
{
   var record = foundset.getRecord(i);
   a[i-1] = new Array(2); // entering the next "dimension"
   a[i-1][0] = record.fieldA // puts the content of fieldA into position 0 of your new Array(2) that sits in position [i-1] of Array a
   a[i-1][1] = record.fieldB // puts the content of fieldB into position 1 of that arry
}

Now you have created a two dimensional array. So what do you want to do with that?

one more remark. This example

a*[j] = “[” + catkey + “,” + tick + “]”; [/quote]*
is a little misleading. It creates a typical array representation (the brackets []) by creating a string value “[” + catkey + “,” + tick + “]”. The expression “[” + catkey + “,” + tick + “]” has nothing to do with Arrays by itself, it’s just a string.

Here’s another attempt at explaining the purpose of this routine and its conditions.

In the current set of client records I’m enabling the end user to choose one record and apply its “settings” to the foundset. Each client has more than one settings record – can range from just two or three to 100 or more, stored in a separate table linked through a relationship to the client id. These settings records have two critical fields – cat key, a text field, and tick, an integer field; as well as the client id field necessary for the relationship.

I’ve been assuming the best way to accomplish the task is to first assemble the target settings into a two-dimensional array – catkey and tick in a series of pairs. Then cycle through the array, catkey field by catkey field finding records that match each of the target catkeys, then imposing the tick field to either a 1 or 0 as defined in the original settings record.

Thus there may be 25 records with “L1C1” in the “catkey” field. I need to “grab” those 25 and set the “tick” field to either 1 or 0, depending on what’s in the L1C1 record in the original target set of “settings standards”. The next catkey field might produce 30 matches. And so on through the array.

In the end I have two problems. 1. Get the catkey and tick values of the user’s choice for “settings standards” into an array in a form that’s easy to access afterwards. 2. Then fetch each pair out of the array so I can search and impose for similar records.

I know I’ve bitten off a difficult biscuit (difficult to me). From a standing start last June I’m still picking up speed in writing JS routines. I’m still quite green around the edges. :wink:

Please feel free to ask supplemental questions and/or propose a completely different routine that would accomplish the same goal. Thanks for your attention to this. Appreciated.