0

one of my first posts, so do not expect too much of me please.

I have an array

string phrase = Value.Text;
string[] words = phrase.Split('\t', '\r');

Which splits the output by tab or return.

I then re-arrange that array to create an output - this caused some confusion so updating...

The Input is from a Spreadsheet, as an example, the array will contain the number for each of the items, So:

TV is Four

Video is Five

Radio is Seven

string TV = words[3];
string Video = words[2];
string Radio = words[1];

Then create an output:

this.OutPut.Text = NumberValue.Text;
this.OutPut.Text += '\t';
this.OutPut.Text += TV;
this.OutPut.Text += '\t';
this.OutPut.Text += DatePicker.Text;

The NumberValue comes from a TextBox - imagine this is the price

The DatePicker comes from a Date picker - imagine this is the date that the information was created (not the day in which it is entered)

The purpose is to copy the data to a template spreadsheet. However, the data must match the destination template spreadsheet - hence the array and output re-arranging.

This has been fine, while dealing with a small array, and a defined length array works fine - for example, just TV, Video and Radio.

I am now seeking to have an array 'upto' 100, but could be less.

I realise that this code is not good as it is going to be 200 lines of code and it also throws "index out of range" exceptions whenever an array is created that does not reach the last words [100] created or output.

So I was wondering if anyone had a better way of doing this? I don't need you to write the code, but give me an idea, and I can go learn...but at the moment, I don't really know what I am looking for or how to search for it, as I doubt I have the language required to find it.

So any pointers gratefully received.

I hope the updates help explain the problem.

6
  • Are you just trying to reverse the array? Then just use Array.Reverse(words);String.Join('',words); Commented Aug 24, 2020 at 22:34
  • No, that was just an example rather than the full code, the full code moves all sorts of data around so that it can be output in a specified list of A, 1, C, 4, F 2, R, 9, P, 7 etc. Commented Aug 24, 2020 at 22:36
  • 1
    It would be helpful to see a better/fuller example of how you want to arrange your data. Commented Aug 24, 2020 at 23:03
  • I have updated the code (please ignore the TV = Four, Video = Five, Radio = Seven - it wouldn't let me post the changes until I made that code... Commented Aug 24, 2020 at 23:24
  • 1
    This question is unclear, you just need concrete examples of input and output data, and what you are trying to achieve. The question seems to be filled with a lot of non technical noise, whereas it just needs facts and examples and an actual technical description of the problem Commented Aug 25, 2020 at 0:08

2 Answers 2

1

Without knowing more about your input/output, it's really hard to make useful suggestions.

Here's an idea that you might find useful, though. Use a List<string> to hold the order of the new items, then join them together when you need to combine then into one line.

Big picture idea:

string phrase = Value.Text;
string[] words = Value.Text.Split(new char[] { '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

List<string> values = new List<string>();
// add things to values, from words, in whatever order you need them to be
values.Add(words[3]);
values.Add(words[4]);
values.Add(words[1]);
string newLine = String.Join("\t", values);
OutPut.AppendText(newLine + Environment.NewLine);

If you already know the order of the indices, the "3", "4", "1" in my example, then you could use an ARRAY to hold those values and then iterate over them:

string phrase = Value.Text;
string[] words = Value.Text.Split(new char[] { '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

int[] newIndexOrder = { 3, 4, 1 };
List<string> values = new List<string>();
foreach(int i in newIndexOrder )
{
    values.Add(words[i]);
}
string newLine = String.Join("\t", values);
OutPut.AppendText(newLine + Environment.NewLine);

string phrase = Value.Text;
string[] words = Value.Text.Split(new char[] { '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

From the comments:

if every 12 values/words I need a line break?

I've added in a StringBuilder to accumulate the output, adding in a break whenever we hit twelve items:

int[] newIndexOrder = { 3, 4, 1 };
List<string> values = new List<string>();
StringBuilder sb = new System.Text.StringBuilder();
foreach(int i in newIndexOrder)
{
    values.Add(words[i]);
    if (values.Count == 12)
    {
        sb.AppendLine(String.Join("\t", values));
        values.Clear();
    }
}            
if (values.Count > 0)
{
    sb.AppendLine(String.Join("\t", values));
}
OutPut.AppendText(sb.ToString());
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you, I have the idea that the second example is going to answer my problem, I just need to read and work through it a little and then test it out. Thanks for taking the time!
That is a really elegant solution and so much better than the one I was using....I have one question....if every 12 values/words I need a line break? I tried adding it, but it won't allow a char in a string. I tried to make a new variable char newLine; newLine = '\r'; but it still wouldn't allow it.
@DravinianHSHearthstone Try string newLine = "\r";
Thanks Martheen, that is a perfect answer.....annoying that '\r' didn't work, but I should have tried " should have seen the mad code I was trying to write to make this work....wasn't working.
See new code at the bottom for your every 12th question.
|
0

You can use a foreach loop and avoid writing 200 lines of code.

Example :

string phrase = Value.Text;
string[] words = phrase.Split('\t', '\r');

foreach (string word in words)
{
    this.OutPut.text += word + '\t';
}

5 Comments

Thanks for that I will try it now.
That would output my array in the same way in which it was entered? How would I re-arrange the array?
this.OutPut.text = string.Join("\t", words) + "\t"; instead of loop. When looping you create new string in each loop which can be time consuming
Yeah I see that i can use + to dispose of one of the Output.Text values which will limit the lines of code - appreciate that bit of code.
Update GUI control on each iteration is very inefficient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.