Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session

Have over 150 variables. How do I declare an array?

Avatar

Level 2

We constructed a form a few years back that uses 35 separate subforms to display a possibility of 35 mutual funds in a list.

Each subform only displays if there are values in the subform(s).

These subforms list the 1 main fund family then a dropdown with 5 alternative funds to choose form.

There are 12 different portfolio mixes - so we reuse 210 variables in 12 different combinations.

Very cool (but poorly written)

I am working on rebuilding this form by eliminating the 35 subforms and adding an instance of the the subform for each fund family.

Then - if the fund family variable is not null, then an instance is created and visible.

I also want to eliminate the 210 variables.

Long story short:  (I suck at arrays)

How do I declare an array so I can store up to n number of values?

Then (to go further) - how to I reference each value individually so I can populate in each object as needed?

To make this complete:

35 variables currently store the main fund family (1 fund family per variable)

Each of these fund families have up to 5 alternate funds.

All you genius(es) out there... UNITE!!

5 Replies

Avatar

Level 10

How do I declare an array so I can store up to n number of values?

Answer: You need not mention the size during declaration:

var myVariable = new Array();

you can add any number of items to the array like:

myVariable[0] = "some data";

myVariable['some string index'] = "some info";

both the above statements are valid.

how to I reference each value individually so I can populate in each object as needed?

In your case, you need upto 35*5 records to store in an array. Hence, try to create a two dimensional array as shown below:

var myVar = new Array();

myVar[1] = new Array();

myVar[2] = new Array();

myVar[3] = new Array();

myVar[4] = new Array();

.

.

.

myVar[35] = new Array();

//and then

// for e.g  The fund family1 has five funds

myVar[1][1] = 'fund name1';

myVar[1][2] = 'fund name2';

myVar[1][3] = 'fund name3';

myVar[1][4] = 'fund name4';

myVar[1][5] = 'fund name5';

// And a fund family with just 3 funds will look like

myVar[2][1] = 'fund nameX';

myVar[3][2] = 'fund nameY';

myVar[4][3] = 'fund nameZ';

// And you can access the array as usually

for(var i=0;i<myVar.length;i++)

{

       for(var j=0;j<myVar[i].length;j++)

       {

             // Your logic to read/ calculate/ create instance and so on...

       }

}

Hope this will help you!

Nith

Avatar

Level 2

I love you!!

To finish my problem - can a list of values be used all at once instead of declaring each value one at a time?

Currently, we use a spreadsheet that our business partners complete with their fund families (35) and alternate funds (up to 5 per fund family).

This spreadsheet then populates 'values' based on the business data - into another tab worksheet that formats the values into a LCD format.

From this format - we can copy for the worksheet and paste into a script object - updating the variables in about 2 minutes.

What I have noticed is that if I use an array - I need to set the values one at a time:

myVar[1][1] = 'fund name1';

myVar[1][2] = 'fund name2';

myVar[1][3] = 'fund name3';

myVar[1][4] = 'fund name4';

myVar[1][5] = 'fund name5';

Is it possible to set a variable (or an array) to accept the values in one string (eg. 'fund name 1', 'fund name 2', fund name 3', fund name 4', 'fund name 5') - then set the values to each 'array set' in code?

I am trying to eliminate the need to have 35x5 variables declared in my form - as well as make it more scalable.  (in case we add more values later)

Something like:

myVar = new Array('fund name 1', 'fund name 2', fund name 3', fund name 4', 'fund name 5');

in this example - maybe I need a different array to hold the value string?

myVar[1] = new Array();

myVar[2] = new Array();

myVar[3] = new Array();

myVar[4] = new Array();

for (var i=1; i< myVar.length; i++)

{

     for (var j=1; j< myVar.length; j++)

     {

          myVar[i][j] = myVar[j];

     }

}

Thanks for the help.

Hope you can keep the answers coming.

Avatar

Level 10

You can replace the following lines into a single line:

myVar[1][1] = 'fund name1';

myVar[1][2] = 'fund name2';

myVar[1][3] = 'fund name3';

myVar[1][4] = 'fund name4';

myVar[1][5] = 'fund name5';

which is equivalent to:

myVar[1]= new Array('fund name1', 'fund name2', 'fund name3', 'fund name4', 'fund name5');

I don’t think you need any more variables to hold your spreadsheet data. Make sense?

-Nith

DISCLAIMER:

========================================================================

"This communication is intended only for the named recipient and others authorized to receive it.

It contains confidential or legally privileged information. If you are not the intended recipient, please notify us immediately, and note that any disclosure, copying, distribution or action you may take in reliance on this communication is strictly prohibited and may be unlawful. Unless indicated otherwise, this communication is not intended, nor should it be taken to create any legal and/or contractual relation or otherwise. Ministry of Finance (MOF) is neither liable for the proper and complete transmission of the communication, nor for any delay in its receipt.

Whilst MOF undertakes all reasonable efforts to screen outgoing e-mails for viruses, it cannot be held liable for any viruses transmitted by this e-mail."

Avatar

Level 2

I think I have on elast question on this topic.

myVar[1]= new Array('fund name1', 'fund name2', 'fund name3', 'fund name4', 'fund name5');

I will still be pulling the values string from a spreadsheet and copying into the script object.

I want to do something like:

fundlist.value='fund name1', 'fund name2', 'fund name3', 'fund name4', 'fund name5';  (this comes from the spreadsheet)

myVar[1]=new Array(fundlist.value);

Unfortunately, I can't get it to work.

Any nuggets of additional gold you can share?

Avatar

Level 10

If you can past the CSV data like the format,

var fundlist = 'fund name1,fund name2,fund name3,fund name4,fund name5,etc'; // assuming these data are coming from spreadsheet

you could initialize the array using the following script:

var myVar[1] = fundlist.split(","); // This code will split the values separated by commas and automatically initialize the array

Hope this may help you!

Nith