converting nested loops into a shorter algorithm

accessuser1023

Registered User.
Local time
Today, 10:58
Joined
Nov 12, 2012
Messages
71
all,



I have this block of code:

Code:
        If ctr = 4 Then
          For block = 1 To 9
            For ctr2 = 1 To 3 'each line.
              For ctr3 = 1 To 3 'char 1 - 3 in each line.
                Select Case ctr2 'which line?
                  Case 1
                    strOut = strOut & Mid(Line1, ctr3, 1)
                  Case 2
                    strOut = strOut & Mid(Line2, ctr3, 1)
                  Case 3
                    strOut = strOut & Mid(Line3, ctr3, 1)
                End Select
              Next ctr3
            Next ctr2
          Next block
        End If

what I'd like to do is streamline it. There are 3 lines of text being captured here. What is (supposed to be) happening is that each block of characters (3 characters per line positions, starting at position 1 or positions every 3 char increments after that) is being captured until all 9 chars are stored in strOut.


9 chars = 3 chars on each line * 3 lines.


is there any better way to do this mathematically? I realize that we can manipulate the variables and coding many different ways, but I'm not interested in that. I'm only interested in using math or leveraging techniques to speed up the process here. I'm trying to focus on the massive iteration and turning it into something more useful and less convoluted.



comments welcome. thanks!
 
Hello accessuser1023, can you give a simple example of the data that this piece of code takes in and the desired output?
 
You say "supposedly" so can I assume that it is not working?

I do not understand what the For Block is for it merely causes the inner loops to be repeated 9 times with no change to any of the variables.

The inner loops seem to produce the result
Strout = left(line1,3) & left(line2,3) & left(line3,3).

This is from examining the code not testing, I agree with Paul we need to see data and expected result as there are contradictions in your post

Brian
 
You say "supposedly" so can I assume that it is not working?

I do not understand what the For Block is for it merely causes the inner loops to be repeated 9 times with no change to any of the variables.

The inner loops seem to produce the result
Strout = left(line1,3) & left(line2,3) & left(line3,3).

This is from examining the code not testing, I agree with Paul we need to see data and expected result as there are contradictions in your post

Brian
hello guys,

I know the code isn't perfect. It's not supposed to be. what it is *supposed* to do is take 9 blocks of 3-chars-each from each of the 3 lines, one block at a time. make sense?

I'm not going to change the code, you get the idea. but here is a sample of the 3 lines of text I have:

Code:
1.2.3.4.5.6.7.8.9.10.11.12.
1+2+3+4+5+6+7+8+9+10+11+12+
1-2-3-4-5-6-7-8-9-10-11-12-

so the first iteration of the loop here would produce the following in "strOut":\

Code:
1.21+21-3

first 3 first line + first 3 second line + first 3 third line.

it reality, the code should do this for all 9 blocks of 3 characters (there are 27 char positions on each line).

thanks!
 
Untested

Strout = NULL
Sp=1
For block = 1 to 9
Strout = strout & mid(line1,sp,3) & mid(line2,sp,3) & mid(line3,sp,3)
Sp=sp+3
Next block


Remember to do your Dim


Brian
 
Hmmm I get a different result, If I run your code.. If I use the same input..
Code:
1.2.3.4.5.6.7.8.9.10.11.12.
1+2+3+4+5+6+7+8+9+10+11+12+
1-2-3-4-5-6-7-8-9-10-11-12-
I get this as the output..
Code:
1.21+21-21.21+21-21.21+21-21.21+21-21.21+21-21.21+21-21.21+21-21.21+21-21.21+21-2
but based on the logic the result should be..
Code:
1.21+21-2.3.+3+-3-4.54+54-5.6.+6+-6-7.87+87-8.9.+9+-9-10.10+10-11.11+11-12.12+12-
The code can be simplified by..
Code:
Public Sub testAWF(Line1 As String, Line2 As String, Line3 As String)
Dim block As Integer, strOut As String
For block = 1 To 27 Step 3
    strOut = strOut & Mid(Line1, block, 3) & Mid(Line2, block, 3) & Mid(Line3, block, 3)
Next
Debug.Print strOut
End Sub
Hope this helps !

EDIT: Oops, Brian has the similar solution.. I hope I have got the logic right..
 
thanks Brian.


but again, I'm really interested in a mathematical algorithm of somekind that you can map to this whole thing. I know we can shorten the syntax (as you've just done), but is there any math you can apply to this in the form of searching out the characters you want? What we've just done at this point is streamlined the iterations so they don't have to happen. and that's fine. I can use that as well. but I'm looking for math possibilities (like possible algorithmic code to use). I know it doesn't make much sense to do so, but it would be great to see some possible uses.


help me out on this too, if you will Brian. is what I'm saying making any sense at all?
 
Paul they are basically the same but I think I prefer yours incorporating the sp= sp+ 3 into the For Block, neat.

Brian
 
thanks Brian.


but again, I'm really interested in a mathematical algorithm of somekind that you can map to this whole thing. I know we can shorten the syntax (as you've just done), but is there any math you can apply to this in the form of searching out the characters you want? What we've just done at this point is streamlined the iterations so they don't have to happen. and that's fine. I can use that as well. but I'm looking for math possibilities (like possible algorithmic code to use). I know it doesn't make much sense to do so, but it would be great to see some possible uses.


help me out on this too, if you will Brian. is what I'm saying making any sense at all?

I'm inclined to say that what you are asking makes no sense, but I'm too long in the tooth to believe that anything is impossible, I just don't see the point other than as some intellectual exercise , not that there is anything wrong with that after all that is why I still come here 7 years after retiring.
It is beyond my brain power.
:D

Best of luck

Brian
 
I have to agree with Brian, I do not tend to get what you actually want.. :confused:
 
Guys,

the "beyond the brain power" comment is probably right. there is a way, and yes it is for intellectual reasons, however it certainly is more difficult no doubt about it. we'll see if any other comments come along. thanks Brian. I will use yours unless something else comes along.
 
taking brians initial stab

Strout = NULL
Sp=1
For block = 1 to 9
Strout = strout & mid(line1,sp,3) & mid(line2,sp,3) & mid(line3,sp,3)
Sp=sp+3
Next block

instead of using sp=sp+3 in each iteration, surely you can just say this.
is that what you mean. finding the start pos, based on the value of the iteration

Code:
Strout = ""
For block = 1 to 9
      Strout = strout & mid(line1,block*3-2,3) & mid(line2,block*3-2,3) & mid(line3,block*3-2,3)
Next block
 
Dave look at Paul's solution which I have said that I prefer to mine, and fwiw I prefer my 1 simple calculation per loop to your 3 slightly more complex ones, it seems like being clever for the sake of it.

Brian
 
Last edited:
Sometimes my brain works extremely slowly, but it does keep going and whilst watching something mindless on TV I suddenly realised that in Paul's solution the For Block should be 1 to 25 step 3 ; not 1 to 27

Brian
 

Users who are viewing this thread

Back
Top Bottom