Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help speeding up a loop

Posted on 2008-06-14
3
Medium Priority
?
189 Views
Last Modified: 2010-04-23
Hi All

The following loop that loops through and generates all possible postcodes in the UK.

How can I change the attached code so that the outer part of each loop is not repeated?

Ie.

The loop has 4 parts...

The outer loop goes through a list of districts....
             
     The Next part generates (1) 1 Digit
         The inner part will Generate 1 Digit followed by 2 Characters

     The Next part generates (2) 2 Digits
         The inner part will Generate 1 Digit followed by 2 Characters

     The Next part generates (3) 1 Digit & 1 Character
         The inner part will Generate 1 Digit followed by 2 Characters


How would I remove the repeating outer part from each loop?

So that I would only require 1 loop that does something along the lines of

The outer loop goes through a list of districts....
             The Next part generates (1) 1 Digit
             The Next part generates (2) 2 Digits
             The Next part generates (3) 1 Digit & 1 Character
                          The inner will Generate 1 Digit followed by 2 Characters

The main thing is though I still have to be able to have each loop outputing the format like so

Distric    1 Digit     1 Digit & 2 Characters
Distric    2 Digit     1 Digit & 2 Characters
Distric    1 Digit & 1 Character     1 Digit & 2 Characters

Any help would be great

many thanks
Steve
For Each r1 As String In District
            s1 = r1
 
            For Each r2 As String In Digit1
                s2 = s1 & r2 & " "
                For Each r3 As String In Digit1
                    s3 = s2 & r3
                    For Each r4 As String In Alpha2
                             '......do stuff
                    Next
                Next
            Next
 
            'Pass 2
            For Each r2 As String In Digit2
                s2 = s1 & r2 & " "
                For Each r3 As String In Digit1
                    s3 = s2 & r3
                    For Each r4 As String In Alpha2
                             '......do stuff
                    Next
                Next
            Next
 
            'Pass 3
            For Each r2 As String In Digit1
                s2 = s1 & r2
                For Each r3 As String In Alpha1
                    s3 = s1 & s2 & r3 & " "
                    For Each r4 As String In Digit1
                        s4 = s3 & r4
                        For Each r5 As String In Alpha2
                             '......do stuff
                        Next
                    Next
                Next
            Next
        Next

Open in new window

0
Comment
Question by:Stephen Manderson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 21784672
in short, 2 methods:

do the inner loops once, and save the "results" in a collection/array/whatever is fit to hold the information.
then, run the "outer" loop and use that collection/array/whatever to work on it...

OR

do the inner loops, and put the "outer" loop as most-inner loop, so for each item generated, run the "outer loop" ...
0
 
LVL 19

Author Comment

by:Stephen Manderson
ID: 21785228
Thanks, was thinking along them lines.

Generated a list for the district, middle and end :-) got my 300 + mil combinations now.. opened another question about resuming the loop if stopped if you can help?
0
 
LVL 19

Author Comment

by:Stephen Manderson
ID: 21785282
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question