Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array problem

Status
Not open for further replies.

Niv3k

Programmer
Jul 11, 2001
350
US
I am having a funny array problem:

I build a multi-dimensional array: a(250,10) Most of the elements don't really use any index in the send dimension(10) past 0. this leaves null values in 1-10. When I try to loop through the array, and stop looping when I come to a null value, it still pulls out values (always 4, to be specific, no matter how many values are used in the array)

Anyone have any ideas?

Kevin
 
Code:
<script language=javascript>

a=new Array(209,10);
var docIndex = -1;
var lastCompany = -1;
for (var d=0;d<=209;d++) {
	for (var c=0;c<=10;c++) {
		a[d,c]=null;
	}
}
function newCat(txt,companyID){
	if (companyID==lastCompany) {
		docIndex++;
	} else {
		docIndex=0;
	}
		a[companyID,docIndex] = txt;
	lastCompany = companyID;
}

newCat ('Change of Status',0);
newCat ('Voucher and Instructions',2);
newCat ('Voucher and Instructions',3);
newCat ('Voucher and Instructions',4);
newCat ('Termination from Service',40);
newCat ('Enrollment Form',40);
newCat ('Voucher and Instructions',40);
//... ad nauseum ...

function ShowNotes () {

	for (i=(CompanyForms.companydocs.length-1);i>-1;i--) {
		CompanyForms.companydocs.remove(i);
	}
	for (i=0;i<10;i++) {
		if (a[CompanyForms.company.value,i]) {
			[CompanyForms.company.value,i]);
			var x = new Option();
			x.text = a[CompanyForms.company.value,i];
			x.value = i;
			CompanyForms.companydocs.add (x);
		}
	}
}

When the user slicks on a select box, the onChange event calls ShowNotes...
 
Your array originally has 2 elements in it, the integers 209 and 10. You're used to using VBScript, I'd guess from the way you wrote this.

You can't access multidimensional arrays in Javascript with [dim1, dim2]. It's easiest, I've found, to assign the element from the first dimension to a separate variable, then access that as a separate array:

for (d1=0;d1<a.length;d1++)
{
var dimension2=a[d1];
for (d2=0;d2<dimension2.length;d2++)
{
dimensions2[d2]=null;
}
}

I've not had the same consistent results with accessing multi-dimensional arrays in JS like you can in C/C++, with a[d1][d2] (note the SEPARATE square brackets), but you could try that, as well.
 
hehe. oops. :)

Thanks, will work on the rewrite now...

Kevin
 
Update:

Ended up finding an article on multidimensional arrays where the guy talked about using object constructors instead. so what I did is:
Code:
<script language=javascript>

var oCompanyDocs = new Array(10)

function Company(ID, documentname) {
    this.company = ID;
    this.docname = new Array(10)
    for (var i=0; i<documentname.length; i++) {
        this.docname[i] = documentname[i];
    }
}

var oCompany = new Array(200)
oCompany[0] = new Company(39, ['Voucher and Instructions'])
oCompany[1] = new Company(40, ['Voucher and Instructions', 'Termination from Service', 'Enrollment Form'])

function ShowNotes () {

    var bool=true;
    var sillystring=&quot;&quot;;
	for (var i=0; bool && oCompany[i]; i++) {
            if (oCompany[i].company == CompanyForms.company.value) {
                for (var c=0; c<oCompany[i].docname.length; c++) {
		    sillystring+=oCompany[i].docname[c]+&quot;  &quot;;
		}
		bool=false;
            }
        }
	alert (sillystring);
}

</script>
thanks so much, trollacious. Anyone know why I can't mark a post as helpful?
 
You can't give me a star because I'm just a visitor. You can only give stars to members. :)# (bearded smiley face)

Objects are really the best way to do things, but it takes a certain mindset to use them effectively and easily.

As a variation to your code, I wouldn't use numbers in the Array() declarations. You don't need to, and can expand them at any time without a problem.

Also, I use this format, and you can change things around, or add and delete more easily:

var oCompany = new Array(), oi=0;

oCompany[oi++] = new Company(39, ['Voucher and Instructions']);
oCompany[oi++] = new Company(40, ['Voucher and Instructions', 'Termination from Service', 'Enrollment Form']);
 
while I like the oi++, The javascript is actually getting produced by php, so, it doesn't really matter if I use it in the javascript or in the php, (I am actually doing that in the php!)

I do like the idea of killing the upper limit. It helps get rid of the 'undefined' in the sillystring.

The other modification I have to make is the document path as an array, also.

Thanks again!

Kevin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top