
For Loop Question
I need to get this thing to loop through each array and give back the results:<BR><BR>Photo1<BR>Photo1Name<BR>Photo2<BR> Photo2Name<BR>Photo3<BR>Photo3Name<BR><BR>I have simplified this below<BR><BR><% <BR>UserImages = Array( "Photo1", "Photo2", "Photo3" ) <BR>ImageNames = Array( "Photo1Name", "Photo2Name", "Photo3Name" ) <BR><BR>For i = 0 To UBound(UserImages) <BR> For j = 0 To UBound(ImageNames)<BR>Response.Write UserImages(i) & "<BR>" <BR>Response.Write ImageNames(j) & "<BR>" <BR> Next <BR>Next <BR>%> <BR><BR>But instead I get:<BR><BR>Photo1<BR>Photo1Name<BR>Photo1<BR>Phot o2Name<BR>Photo1<BR>Photo3Name<BR>Photo2<BR>Photo1 Name<BR>Photo2<BR>Photo2Name<BR>Photo2<BR>Photo3Na me<BR>Photo3<BR>Photo1Name<BR>Photo3<BR>Photo2Name <BR>Photo3<BR>Photo3Name<BR><BR>I can see how the iterations of the loop is getting out of wack but do not see why.<BR><BR>Ideas?

Why out of whack???
Looks like right results to me. Every element of first array matched with every element of second array. As coded.<BR><BR>What were you expecting???<BR><BR>

RE: For Loop Question
Photo1<BR>Photo1Name<BR>Photo2<BR>Photo2Name<BR>Ph oto3<BR>Photo3Name<BR><BR>is what I expected but got<BR><BR>Photo1<BR>Photo1Name<BR>Photo1<BR>Photo 2Name<BR>Photo1<BR>Photo3Name<BR>Photo2<BR>Photo1N ame<BR>Photo2<BR>Photo2Name<BR>Photo2<BR>Photo3Nam e<BR>Photo3<BR>Photo1Name<BR>Photo3<BR>Photo2Name< BR>Photo3<BR>Photo3Name<BR><BR>Better shown:<BR><BR>Iteration 0 of the loop, myArray(i) is Photo1<BR>Iteration 0 of the loop, myArray(j) is Photo1Name<BR><font color="red">Iteration 0 of the loop, myArray(i) is Photo1</font> <font size="2">should be at Iteration 1 of the loop, myArray(i) is Photo2 </font><BR><BR>Iteration 1 of the loop, myArray(j) is Photo2Name<BR>Iteration 0 of the loop, myArray(i) is Photo1<BR>Iteration 2 of the loop, myArray(j) is Photo3Name<BR>Iteration 1 of the loop, myArray(i) is Photo2<BR>Iteration 0 of the loop, myArray(j) is Photo1Name<BR>Iteration 1 of the loop, myArray(i) is Photo2<BR>Iteration 1 of the loop, myArray(j) is Photo2Name<BR>Iteration 1 of the loop, myArray(i) is Photo2<BR>Iteration 2 of the loop, myArray(j) is Photo3Name<BR>Iteration 2 of the loop, myArray(i) is Photo3<BR>Iteration 0 of the loop, myArray(j) is Photo1Name<BR>Iteration 2 of the loop, myArray(i) is Photo3<BR>Iteration 1 of the loop, myArray(j) is Photo2Name<BR>Iteration 2 of the loop, myArray(i) is Photo3<BR>Iteration 2 of the loop, myArray(j) is Photo3Name<BR><BR><BR>So instead I can just do below for I know that it will always be For i = 0 To 2.<BR><BR>But still... seems like the nested loop should have worked. <BR><BR><% <BR>UserImages = Array( "Photo1", "Photo2", "Photo3" ) <BR>ImageNames = Array( "Photo1Name", "Photo2Name", "Photo3Name" ) <BR><BR>For i = 0 To 2 <BR>Response.Write UserImages(i) & "<BR>" <BR>Response.Write ImageNames(i) & "<BR>" <BR>Next <BR>%>

Dunno why...
...you would think the nested loop would work.<BR><BR>Here. Look at it without bothering with the arrays:<BR><BR><%<BR>For i = 0 To 2<BR> For j = 0 To 2<BR> Response.Write "i=" & i & ", j=" & j & "<br/>"<BR> Next<BR>Next<BR>%><BR><BR>Go on, actually try that.<BR><BR>See?<BR><BR>Yes, your final version is correct, though you might have coded:<BR><BR><% <BR>UserImages = Array( "Photo1", "Photo2", "Photo3" ) <BR>ImageNames = Array( "Photo1Name", "Photo2Name", "Photo3Name" ) <BR><BR>If UBound(UserImages) <> UBound(ImageNames) Then<BR> Response.Write "FATAL ERROR: Array sizes not same!"<BR> Response.End<BR>End If<BR><BR>For i = 0 To UBound(UserImages)<BR> Response.Write UserImages(i) & " :: " & ImageNames(i) & "<br/>" <BR>Next <BR>%><BR>

RE: For Loop Question
Bill should have caught this one :o)<BR><BR>Maybe im wrong?<BR><BR>It's because for each loop you do on Array userimages<BR>you do a loop through all the ImageNames<BR><BR>If you know that the number of elements are the same in each array then you dont need to loop through the 2nd array...<BR><BR><%<BR>For i = 0 To UBound(UserImages) <BR>Response.Write UserImages(i) & "</br>" <BR>Response.Write ImageNames(i) & "</br>" <BR>Next <BR>%> <BR>See it?<BR>The 2nd array should match on the name number of the first array...<BR><BR>Question...?<BR>Are you getting these from a Database?<BR>Using getrows will give you an array of records which might be more efficien...<BR><BR>OR<BR><BR>if not, maybe building a 2D array might work better for you.<BR><BR>Hope this helps.

RE: Dunno why...
For:<BR><BR>For i = 0 To 2 <BR> For j = 0 To 2 <BR> Response.Write "i=" & i & ", j=" & j & "<br/>" <BR> Next <BR>Next <BR><BR>This what I got:<BR><BR>i=0, j=0<BR>i=0, j=1<BR>i=0, j=2<BR>i=1, j=0<BR>i=1, j=1<BR>i=1, j=2<BR>i=2, j=0<BR>i=2, j=1<BR>i=2, j=2<BR><BR>hmmm,<BR><BR>I like your version of checking the UBounds first to make sure tahy are the same.<BR><BR>

I did catch it...
...but I thought he coded it that way on purpose.<BR><BR>Why would you use nested loops if you didn't want nested results???<BR><BR>Personally, I'd avoid building a 2D array except via GetRows. There's not convenient way to do so in VBS code.<BR><BR>You *can* produce an arrayofarrays easily:<BR><BR>Dim Images<BR>Images = Array( _<BR> Array("Photo1Name", "Photo1URL" ), _<BR> Array("Photo2Name", "Photo2URL" ), _<BR> Array("Photo3Name", "Photo3URL" ), _<BR> )<BR><BR>This is the same kind of arrayofarrays you must work with in JS coding, so it's easy to get comfortable using this, in both languages.<BR><BR>So then you would do:<BR><BR><%<BR>For i = 0 TO UBound(Images)<BR> imageName= Images(i)(0)<BR> imageURL = Images(i)(1)<BR> ... whatever ...<BR>Next<BR>%><BR><BR>

So do you understand the point?
Essentially, when you do nested loops like that, you get *EVERY* possible PERMUTATION of the elements.<BR><BR>In fact, consider this code:<BR><BR>For i = 1003 To 1005<BR> For j = 7 To 11<BR> Response.Write "i=" & i & ", j=" & j & "<br/>" <BR> Next <BR>Next <BR><BR>Does that drive it home a bit better? Since now the two loops show up as independent? Which they really were all along, but maybe not so obviously.

RE: For Loop Question
Yes saw that all after i posted...<BR><BR>only reason I mentioned 2darrays was so that while taking special care to build it proper, he's assured to have equal elements for both side...<BR>get rows would work better...<BR><BR>Not sure what he wants exactly<BR>but either 2 would be good.<BR><BR>to list the Photo1 followed by "All the Image Names"<BR>then the next photo2 followed by all the image names<BR>then you should have response.write Response.Write UserImages(i) "Before" starting the 2nd loop...<BR><BR><% <BR>UserImages = Array( "Photo1", "Photo2", "Photo3" ) <BR>ImageNames = Array( "Photo1Name", "Photo2Name", "Photo3Name" ) <BR><BR><%<BR>For i = 0 To UBound(UserImages) <BR>Response.Write UserImages(i) & "</br>" 'Will write 1 time then next loop<BR><BR>For j = 0 To UBound(ImageNames) <BR>Response.Write ImageNames(j) & "</br>" <BR>'Will write all names before next Photo2<BR><BR> Next <BR><BR><BR>Next <BR>%> <BR>

Get It
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

