473,320 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Comparing Multidimensional Arrays

Hello everybody, my question is this:
I have two multidimensional arrays and I have to create a third one (for later use) from comparing these two. Here is my example code:
Expand|Select|Wrap|Line Numbers
  1. //BEGIN CODE
  2. var myGroups = [["00002","00003","00004","00005"],["00007","00008"],["00009","00010"]];
  3. var myProducts = [["00002"],["00003"],["00007"],["00009"]];
  4. var newGroups = new Array();//Resulting Array
  5. function main() {
  6.  
  7. for(i=0; i<myGroups.length; i++){
  8.  
  9. var myGroup = myGroups[i];
  10.  
  11. for(k=0; k<myGroup.length; k++){
  12.  
  13. for(j=0; j<myProducts.length; j++){
  14.  
  15. if(myProducts[j]==myGroup[k]){
  16.  
  17. newGroups.push(new Array(myProducts[j]));
  18.  
  19. }
  20. }
  21. }
  22. }
  23. alert (newGroups[0]);
  24.  
  25. }
  26. main();
  27.  
This way I get a third multidimensional array (newGroups) which contains 1 line arrays, while I want to maintain the grouping structure of the first multidimensional array ([["00002","00003"],["00007"],["00009"]]

Hope you understand the problem..
Any suggestions would be very appreciate
Thanks
Nov 1 '08 #1
12 5594
acoder
16,027 Expert Mod 8TB
If you want to keep the structure, create the array outside the third nested loop and populate within it (if statement - line 15-17), then push the array outside that loop.
Nov 2 '08 #2
Thank you acoder, I tried what you're saying but I'm afraid I'm doing something wrong - Should you please hard code your solution? It would be very helpful for me (I'm not an expert coder..). Anyway, thank you so much for your help.
Nov 2 '08 #3
acoder
16,027 Expert Mod 8TB
Can you show me what you tried?
Nov 3 '08 #4
Sure, here's my attempt:
Expand|Select|Wrap|Line Numbers
  1. //BEGIN CODE
  2. var myGroups = [["00002","00003","00004","00005"],["00007","00008"],["00009","00010"]];
  3. var myProducts = [["00002"],["00003"],["00007"],["00009"]];
  4. var resultingGroups = new Array();//Resulting Array
  5. function main() {
  6.     for(i=0; i<myGroups.length; i++){
  7.         var myGroup = myGroups[i];
  8.         for(k=0; k<myGroup.length; k++){
  9.             var newGroup = new Array();//Array outside third nested loop
  10.             for(j=0; j<myProducts.length; j++){
  11.                 if(myProducts[j]==myGroup[k]){
  12.                     newGroup.push(new Array(myProducts[j]));//Populates newGroup
  13.                 }
  14.             }
  15.             resultingGroups.push(newGroup);//Push resultingGroups with newGroup
  16.         }
  17.     }
  18. }
  19. main();
  20. alert (resultingGroups[0]);
  21.  
Nov 3 '08 #5
acoder
16,027 Expert Mod 8TB
I haven't tested your code, but I noticed one thing. On line 12, you're pushing an array making newGroup into a multi-dimensional array. Just push myProduct[j] instead.
Nov 3 '08 #6
Hello acode, I tried to modify the code as you suggested me; I also expanded a little the code to make the result more evident; what I get is something like this:

Comparing Multidimensional Arrays

myGroup_0
00002
00003
00004
00005
myGroup_1
00007
00008
myGroup_2
00009
00010
resultingGroup_0
00002
resultingGroup_1
00003
resultingGroup_2
resultingGroup_3
resultingGroup_4
00007
resultingGroup_5
resultingGroup_6
00009
resultingGroup_7
[HTML]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="it">
<head><h2>Comparing Multidimensional Arrays</h2></head>
<body>
<script language="JavaScript">
<!--
//BEGIN CODE
var myGroups = [["00002","00003","00004","00005"],["00007","00008"],["00009","00010"]];
var myProducts = [["00002"],["00003"],["00007"],["00009"]];
var resultingGroups = new Array();//Resulting Array
function main() {

for(i=0; i<myGroups.length; i++){
var myGroup = myGroups[i];
for(k=0; k<myGroup.length; k++){
var newGroup = new Array();//Array outside third nested loop
for(j=0; j<myProducts.length; j++){
if(myProducts[j]==myGroup[k]){
newGroup.push(myProducts[j]);//Populates newGroup
}
}
resultingGroups.push(newGroup);//Push resultingGroups with newGroup
}
}

}
main();
for(i=0; i<myGroups.length; i++){
document.write("<br>"+"myGroup_"+i);
var myGroup = myGroups[i];
for(k=0; k<myGroup.length; k++){
document.write("<br>"+myGroup[k]);
}
}

for(i=0; i<resultingGroups.length; i++){
document.write("<br>"+"resultingGroup_"+i);
var myGroup = resultingGroups[i];
for(k=0; k<myGroup.length; k++){
document.write("<br>"+myGroup[k]);
}
}
//-->
</script>
</body>
</html>[/HTML]

Have you got any suggestions?

Thanks
Nov 3 '08 #7
acoder
16,027 Expert Mod 8TB
You need to move the array creating and populating lines further out, i.e. move line 17 upwards outside the for loop and line 23 downwards outside the loop.
Nov 3 '08 #8
Great acode, it worked!!!
I include the final code for the sake of completeness:

[HTML]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="it">
<head><h2>Comparing Multidimensional Arrays</h2></head>
<body>
<script language="JavaScript">
<!--
//BEGIN CODE
var myGroups = [["00002","00003","00004","00005"],["00007","00008"],["00009","00010"]];
var myProducts = [["00002"],["00003"],["00007"],["00009"]];
var resultingGroups = new Array();//Resulting Array
function main() {
for(i=0; i<myGroups.length; i++){
var myGroup = myGroups[i];
var newGroup = new Array();
for(k=0; k<myGroup.length; k++){
for(j=0; j<myProducts.length; j++){
if(myProducts[j]==myGroup[k]){
newGroup.push(myProducts[j]);//Populates newGroup
}
}
}
resultingGroups.push(newGroup);//Push resultingGroups with newGroup
}
}
main();
for(i=0; i<myGroups.length; i++){
document.write("<br>"+"myGroup_"+i);
var myGroup = myGroups[i];
for(k=0; k<myGroup.length; k++){
document.write("<br>"+myGroup[k]);
}
}

for(i=0; i<resultingGroups.length; i++){
document.write("<br>"+"resultingGroup_"+i);
var myGroup = resultingGroups[i];
for(k=0; k<myGroup.length; k++){
document.write("<br>"+myGroup[k]);
}
}
//-->
</script>
</body>
</html>[/HTML]

Thank you again for the great support!

Filippo
Nov 4 '08 #9
acoder
16,027 Expert Mod 8TB
No problem at all! Glad to hear that it's working. Remember that should you need help on any JavaScript-related issues in future, you know where to come :)
Nov 4 '08 #10
Actually, there is another question that comes to my mind -
With the actual code, a create a new array (newGroup) even if there is no correspondence between the two starting arrays (myGroups, myProducts). I'm thinking if there is a way to create a new array only when there is a correspondence between the two..
Nov 4 '08 #11
acoder
16,027 Expert Mod 8TB
On line 23, check if the array is not empty before adding it to resultingGroups.
Nov 4 '08 #12
Sure acode, you're right, I should have think it by myself..
finished code attached

[HTML]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="it">
<head><h2>Comparing Multidimensional Arrays</h2></head>
<body>
<script language="JavaScript">
<!--
//BEGIN CODE
var myGroups = [["00002","00003","00004","00005"],["00007","00008"],["00009","00010"]];
var myProducts = [["00002"],["00003"],["00009"]];
var resultingGroups = new Array();//Resulting Array
function main() {
for(i=0; i<myGroups.length; i++){
var myGroup = myGroups[i];
var newGroup = new Array();
for(k=0; k<myGroup.length; k++){
for(j=0; j<myProducts.length; j++){
if(myProducts[j]==myGroup[k]){
newGroup.push(myProducts[j]);//Populates newGroup
}
}
}
if(newGroup.length > 0){
resultingGroups.push(newGroup);//Push resultingGroups with newGroup
}
}
}
main();
for(i=0; i<myGroups.length; i++){
document.write("<br>"+"myGroup_"+i);
var myGroup = myGroups[i];
for(k=0; k<myGroup.length; k++){
document.write("<br>"+myGroup[k]);
}
}

for(i=0; i<resultingGroups.length; i++){
document.write("<br>"+"resultingGroup_"+i);
var myGroup = resultingGroups[i];
for(k=0; k<myGroup.length; k++){
document.write("<br>"+myGroup[k]);
}
}
//-->
</script>
</body>
</html>[/HTML]
Nov 4 '08 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: ubccis | last post by:
Hi. Does for each for on multidimensional arrays? For example, if one element of $in is $course_list and I want to echo all everything in course_list
2
by: Terry | last post by:
Hi, can someone plz tell me how multidimensional arrays (like a 2-D array) are stored in memory? Are they like single dimensional arrays? Stored sequentially in one "row", so to say? Thanks ...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
5
by: whisper | last post by:
Hi: there is an issue that confuses me and the FAQ did not clarify it for me. (Sorry I am just learning!) Let say I define a multidimensional array in my main routine as follows: int...
3
by: matthewburton | last post by:
Hi everyone, I'm trying to write a program that will search a body of text and replace certain words (say, A, B, and C) with different words that I think are more appropriate (A1, B1 and C1). I...
21
by: utab | last post by:
Hi there, Is there a way to convert a double value to a string. I know that there is fcvt() but I think this function is not a part of the standard library. I want sth from the standard if...
9
by: barmy_mad | last post by:
Hi I new to C# and trying to store/extract a collection of multidimensional arrays. For example; string a_DATA = new string object o_CON = new object for(int i=0;i<10;i++){ for(int...
5
by: asdf | last post by:
I was told not to use the low-level language such as arrays which inherited from C, I want to know what can I use to substitute the C-style multidimensional arrays? Is there multidimensional vector?
2
by: oopsatwork | last post by:
Ok...so, I have been outside of the C world for a _very_ long time...but not so long as to remember how to do multidimensional arrays. So, let me state that I know how to malloc pointers to...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.