473,570 Members | 2,995 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comparing Multidimensiona l Arrays

11 New Member
Hello everybody, my question is this:
I have two multidimensiona l 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 multidimensiona l array (newGroups) which contains 1 line arrays, while I want to maintain the grouping structure of the first multidimensiona l array ([["00002","00 003"],["00007"],["00009"]]

Hope you understand the problem..
Any suggestions would be very appreciate
Thanks
Nov 1 '08 #1
12 5631
acoder
16,027 Recognized Expert Moderator MVP
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
filippo nanni
11 New Member
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 Recognized Expert Moderator MVP
Can you show me what you tried?
Nov 3 '08 #4
filippo nanni
11 New Member
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 Recognized Expert Moderator MVP
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
filippo nanni
11 New Member
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 Multidimensiona l 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.dt d">
<html xmlns="http://www.w3.org/1999/xhtml" lang="it">
<head><h2>Compa ring Multidimensiona l Arrays</h2></head>
<body>
<script language="JavaS cript">
<!--
//BEGIN CODE
var myGroups = [["00002","00003" ,"00004","00005 "],["00007","00 008"],["00009","00 010"]];
var myProducts = [["00002"],["00003"],["00007"],["00009"]];
var resultingGroups = new Array();//Resulting Array
function main() {

for(i=0; i<myGroups.leng th; i++){
var myGroup = myGroups[i];
for(k=0; k<myGroup.lengt h; k++){
var newGroup = new Array();//Array outside third nested loop
for(j=0; j<myProducts.le ngth; j++){
if(myProducts[j]==myGroup[k]){
newGroup.push(m yProducts[j]);//Populates newGroup
}
}
resultingGroups .push(newGroup) ;//Push resultingGroups with newGroup
}
}

}
main();
for(i=0; i<myGroups.leng th; i++){
document.write( "<br>"+"myGroup _"+i);
var myGroup = myGroups[i];
for(k=0; k<myGroup.lengt h; k++){
document.write( "<br>"+myGr oup[k]);
}
}

for(i=0; i<resultingGrou ps.length; i++){
document.write( "<br>"+"resulti ngGroup_"+i);
var myGroup = resultingGroups[i];
for(k=0; k<myGroup.lengt h; k++){
document.write( "<br>"+myGr oup[k]);
}
}
//-->
</script>
</body>
</html>[/HTML]

Have you got any suggestions?

Thanks
Nov 3 '08 #7
acoder
16,027 Recognized Expert Moderator MVP
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
filippo nanni
11 New Member
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.dt d">
<html xmlns="http://www.w3.org/1999/xhtml" lang="it">
<head><h2>Compa ring Multidimensiona l Arrays</h2></head>
<body>
<script language="JavaS cript">
<!--
//BEGIN CODE
var myGroups = [["00002","00003" ,"00004","00005 "],["00007","00 008"],["00009","00 010"]];
var myProducts = [["00002"],["00003"],["00007"],["00009"]];
var resultingGroups = new Array();//Resulting Array
function main() {
for(i=0; i<myGroups.leng th; i++){
var myGroup = myGroups[i];
var newGroup = new Array();
for(k=0; k<myGroup.lengt h; k++){
for(j=0; j<myProducts.le ngth; j++){
if(myProducts[j]==myGroup[k]){
newGroup.push(m yProducts[j]);//Populates newGroup
}
}
}
resultingGroups .push(newGroup) ;//Push resultingGroups with newGroup
}
}
main();
for(i=0; i<myGroups.leng th; i++){
document.write( "<br>"+"myGroup _"+i);
var myGroup = myGroups[i];
for(k=0; k<myGroup.lengt h; k++){
document.write( "<br>"+myGr oup[k]);
}
}

for(i=0; i<resultingGrou ps.length; i++){
document.write( "<br>"+"resulti ngGroup_"+i);
var myGroup = resultingGroups[i];
for(k=0; k<myGroup.lengt h; k++){
document.write( "<br>"+myGr oup[k]);
}
}
//-->
</script>
</body>
</html>[/HTML]

Thank you again for the great support!

Filippo
Nov 4 '08 #9
acoder
16,027 Recognized Expert Moderator MVP
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

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

Similar topics

6
15567
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
1137
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 Terry
9
6657
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 function (which was translated from Fortran code), among other heinous and numerous declarations, is this bit: static float bbuff; static int...
5
545
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 arr={{0,1,2}, {3,4,2}}; and I pass the array as a parameter to a function declared by:
3
1862
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 have a feeling that I need to do this through a multidimensional array. Am I on the right track? thanks, matt
21
4163
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 possible. The thing I am trying to do is to convert a double value to a string with 8 elements. 8 is fixed because of the files I work with. I will...
9
2125
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 j=0;j<3;j++){
5
1975
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
4475
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 pointers and then malloc my data space. This question is NOT about that. I need to keep a dynamically sized (a result of command line options) matrix...
0
7726
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7636
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7948
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7700
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6329
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3671
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1238
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
971
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.