Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / ColdFusion / Advanced Techniques / April 2005



Tip: Looking for answers? Try searching our database.

Nested arrays & probability

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Neotoxin - 29 Apr 2005 08:04 GMT
Let say  I have a 2D array;
myarray[1][1]="A"
myarray[1][2]="B"
myarray[1][3]="C"
myarray[2][1]="D"
myarray[3][1]="E"
myarray[3][2]="F"
.
.
myarray[n]="x"

From the 2D array above I want to create 1D array (nextarray),

nextarray[1]=myarray[1][1]+myarray[2][1]+myarray[3][1]+...+myarray[m]
nextarray[2]=myarray[1][1]+myarray[2][1]+myarray[3][2]+...+myarray[m][j]
nextarray[3]=myarray[1][2]+myarray[2][1]+myarray[3][1]+...+myarray[m][k]
nextarray[4]=myarray[1][2]+myarray[2][1]+myarray[3][2]+...+myarray[m][l]
nextarray[5]=myarray[1][3]+myarray[2][1]+myarray[3][1]+...+myarray[m][m]
nextarray[6]=myarray[1][3]+myarray[2][1]+myarray[3][2]+...+myarray[m][o]
.
.
nextarray[n]=myarray[1][a]+myarray[2][ b]+myarray[3][c]+.....+myarray[m][d]

so that I would get, for example,
nextarray[1]=ADE
nextarray[2]=ADF
nextarray[3]=BDE
nextarray[4]=BDF
.
. ... and so on,

ArrayLen for the result should be ArrayLen(nextarray)=ArrayLen(myarray[1]) *
ArrayLen(myarray[2] )* ArrayLen(myarray[3]) * .... * ArrayLen(myarray[n])

This would involve nested loop right? can anyone show me how to make that
loop(s)?

Any help would be aapreciated
Thank You
NTX

nextarray[n]=myarray[1][a]+myarray[2][b]+myarray[3][c]+.....+myarray[m][d]
jdeline - 29 Apr 2005 13:26 GMT
First of all, you can not use the "+" to concatenate two alphabetic strings.  You must use "&":
nextarray[1]=myarray[1][1] & myarray[2][1] & myarray[3][1] &...
Neotoxin - 30 Apr 2005 00:36 GMT
Ok. I'll take note, but can someone show me how to make those loops...
Neotoxin - 30 Apr 2005 07:04 GMT
Btw, I forgot to mention that the same thing can be done using index looping,
if I'm using 1D array that contain lists, for example:
<cfset myarray=ArrayNew(1)>
<cfset myarray[1]="A,B,C,D">
<cfset myarray[2]="E,F">
<cfset myarray[3]="G,H,I">
<cfloop index="p" list="#myarray[1]#" delimiter=",">
          <cfloop index="q" list="#myarray[2]#" delimiter=",">
                  <cfloop index="r" list="#myarray[3]#" delimiter=",">
                            <cfoutput>#p##q##r#<br></cfoutput>
                  </cfloop>
          </cfloop>
</cfloop>

The only problem is, by using that method,  the number of nested loops depend
on the myarray's length. What if the myarray's length is dynamic (could be
1,2,3,...or 100 maybe)?. Is it possible to use a predefined template and use
<cfinclude> inside the loop? If it is, how would I do it?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.