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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding the Length of Multidimensional Arrays.... 4

Status
Not open for further replies.

Edcrosbys

ISP
Apr 26, 1999
112
US
Using a 2-D Array....

like this:

Eric FDOT
Eric Bond, Inc
George FDOT
George JEA
Jon FDOT
Wes Shipyards, Inc

If I do a
alert(ContArray.length);

I get two. How do I get the nuber of rows instead of the number of colums?

Thanks Alot!!
 
You said:
If I do a
alert(ContArray.length);
I get two.


Or did you mean: I get six ???

Because it was confusing to me so I made this example.
So this was the right time for my first attempt to make multi-dimensional array. This is what I found out(my reference is quit sparing with information about it)
Please xutopia or other experts, correct me if I'm wrong :

var ContArray = new Array();
ContArray[0] = new Array();
ContArray[1] = new Array();
ContArray[2] = new Array();
ContArray[3] = new Array();
ContArray[4] = new Array();
ContArray[5] = new Array();

ContArray[0] = ['Eric' , 'AAAA']
ContArray[1] = ['Jim' , 'BBBB']
ContArray[2] = ['George' , 'CCCC']
ContArray[3] = ['Pam' , 'DDDD']
ContArray[4] = ['John' , 'EEEE']
ContArray[5] = ['Wes' , 'FFFF']

var show = 'column1\tcolumn2\n'
show = show + ContArray[0][0] + '\t' + ContArray[0][1] + '\n'
show = show + ContArray[1][0] + '\t' + ContArray[1][1] + '\n'
show = show + ContArray[2][0] + '\t' + ContArray[2][1] + '\n'
show = show + ContArray[3][0] + '\t' + ContArray[3][1] + '\n'
show = show + ContArray[4][0] + '\t' + ContArray[4][1] + '\n'
show = show + ContArray[5][0] + '\t' + ContArray[5][1] + '\n'
show = show + '\n'
show = show + 'column length = ' + ContArray.length + '\n'
show = show + '\n'
show = show + 'row length = ' + ContArray[0].length + '\n'

alert(show);

BTW: \t is the escape sequence for "horizontal tab"

Erik
<!-- My sport: Boomerang throwing !!
This year I will participate at the World Championships in Germany. (!! Many Happy Returns !! -->
 
Boomerang, I automaticaly assumed if he was getting two using ContArray.lenght he should get six using ContArray[0].length. You are right however. It is the other way around.

How do you like this shorthand way of doing the same thing?

var ContArray = [['Eric', 'AAAA'],
['Jim', 'BBBB'],
['George', 'CCCC'],
['Pam' , 'DDDD'],
['John', 'EEEE'],
['Wes', 'FFFF']]

alert('column1\tcolumn2\n'
+ ContArray[0][0] + '\t' + ContArray[0][1] + '\n'
+ ContArray[1][0] + '\t' + ContArray[1][1] + '\n'
+ ContArray[2][0] + '\t' + ContArray[2][1] + '\n'
+ ContArray[3][0] + '\t' + ContArray[3][1] + '\n'
+ ContArray[4][0] + '\t' + ContArray[4][1] + '\n'
+ ContArray[5][0] + '\t' + ContArray[5][1] + '\n'
+ '\n' + 'column length = ' + ContArray.length + '\n'
+ '\n' + 'row length = ' + ContArray[0].length + '\n')

Gary Haran
 
Thanx Gary, that was the missing part of my reference :)

How about this short version (especially you have 100 rows ;-):

var ContArray = [['Eric', 'AAAA'],
['Jim', 'BBBB'],
['George', 'CCCC'],
['Pam' , 'DDDD'],
['John', 'EEEE'],
['Wes', 'FFFF']]

for (i=0; i<ContArray.length; i++)
{sub = sub + ContArray[0] + '\t' + ContArray[1] + '\n'}


alert('column1\tcolumn2\n' + sub
+ '\n' + 'column length = ' + ContArray.length + '\n'
+ '\n' + 'row length = ' + ContArray[0].length + '\n')

Erik <!-- My sport: Boomerang throwing !!
This year I will participate at the World Championships in Germany. (!! Many Happy Returns !! -->
 
fall again in the [ignore][/ignore]-trap :)

This is the right code:

[ignore]
var ContArray = [['Eric', 'AAAA'],
['Jim', 'BBBB'],
['George', 'CCCC'],
['Pam' , 'DDDD'],
['John', 'EEEE'],
['Wes', 'FFFF']]

for (i=0; i<ContArray.length; i++)
{sub = sub + ContArray[0] + '\t' + ContArray[1] + '\n'}


alert('column1\tcolumn2\n' + sub
+ '\n' + 'column length = ' + ContArray.length + '\n'
+ '\n' + 'row length = ' + ContArray[0].length + '\n')
[/ignore]

Erik <!-- My sport: Boomerang throwing !!
This year I will participate at the World Championships in Germany. (!! Many Happy Returns !! -->
 
WOW! My Pure Javascript book only wishes it had this much info about 2-D Arrays! Thanks Alot guys!

~Eric
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top