So I wrote some code and was told it was poor form to write it in this way.
So hoping someone can point me towards a better way. My code:
string phrase = Value.Text;
List<string> wordList = new List<string>();
string words = phrase.Split('\t', '\r');
int Three = 3;
int Two = 2;
int One = 1;
int Four = 4;
int Five = 5;
int Six = 6;
for (int i = 0; i < result; i = i + 12, Three = Three + 12,
Two = Two + 12, One = One + 12, Four = Four + 12, Five = Five + 12,
Six = Six + 12)
{
wordList.Add(ID.Text + '\t');
wordList.Add(IT.Text + '\t');
wordList.Add(words[Three] + '\t');
wordList.Add(words[Two] + '\t');
wordList.Add(PD.Text + '\t');
....etc.
}
The offending part is this:
for (int i = 0; i < result; i = i + 12, Three = Three + 12,
Two = Two + 12, One = One + 12, Four = Four + 12, Five = Five + 12,
Six = Six + 12)
I can see why, it is pretty ugly and quite long, perhaps unnecessarily so?
What are you trying to do?
I have an array of data (phrase) that is entirely random.
The data is entered in sets of 12, but more than one data set can be entered, but always divisible by 12 - so 12, 24, 36. I do not know how much data will be entered at any given point, but I know it will be divisible by 12.
Sometimes words, sometimes numbers - I do not know in advance what it will be, they are not days, they are not ages, I cannot specify an element to the data points in the array. They will be varying in character length, they will have different punctuation marks in different places in each data set. All of the data is separated by a Tab.
From the list of 12, I need to pull, the 3, 2, 1, 4, 5, 6.
In that order. I then place that data into a List<> and output the data, along with some text boxes to match a specified format.
Purpose of the code:
To loop through the data in sets of 12, until there is no more data.
Each loops pulls a data point from the array, for one example, the 3rd, 15th, 27th, 39th etc.
Each loop adds the data from the words array, adds TextBoxes in various places and then compiles them in a List. The List is then produced for the user to copy into a specified format in a single action.
I thought up the "int Three = 3;" for the purpose of being able to increase that number by 12 each loop. I couldn't think of any other way to do it.
But here to learn, so if you know how it should be done - I am all ears, even if you just want to point me to what I should be doing.
If you need an example of the data, face palm your keyboard 12 times putting a tab between each one...you will have an idea of what the data is - there is absolutely no point me providing an example, it is meaningless gibberish, the only factor that is consistent is the Tab between each grouping.
To long for a comment - Here's a prospective solution implementing what I outlined earlier. It breaks the input into 12 length chunks and creates a temporary collection of word sets that could be used for whatever purpose you need with comments explaining how it works.
See https://stackoverflow.com/a/1008974/426894 for more on how the grouping works here.
Input like this:
Generates Output like: