Monday, October 27, 2014

Nested loops

A number of years ago, the company I worked for needed to generate a large set of unique codes. The codes needed to be 5 digits long, and we wanted to remove the letters I and O and the numbers 1 and 0 from the listing.  In an effort to avoid letting my PowerShell skills rust, I decided to recreate this project in PowerShell.

As I started thinking about ways to approach this, it occurred to me that I rarely seem to need to use nested loops.  I assume there are other ways to solve this, but nesting seemed like fun.  Maybe this outline will prove useful for someone looking to learn a little bit more about nesting.

I broke the problem down into two parts:
  1. Create an alphanumeric array that conforms to the requirements (removing the I,O,1, and 0)
  2. Create a series of nested loops, with each loop representing 1 of the places in the 5 digit code
Once finished, the output was written to a .CSV file, and it looked similar to the small sample below:

BH3NA
BH3NB
BH3NC
BH3ND
BH3NE
BH3NF
BH3NG

If you were to let this run to completion, I believe it would generate 24,000,000 + unique codes (this post is about PowerShell, not Finite mathematics).