0

A file is read in. Looks for lines that have a number that beings with an S The lines that do not have an S are maintained. Saves to an array. I am then populating an existing gridview with the same amount of lines.

As a place holder I have set the blank lines to *** This is where I'm stuck. I need the empty strings to be populated with the last non empty string.

So for example if the readout is:

1
2
3
Empty
Empty
Empty
4
Empty
6

I'd want it displayed as:

1
2
3
3
3
3
4
4
6

I can't figure out how to do that. I've been searching all day for examples but can only find ways of grabbing either the first or last number of my array is all. Here is my code.

var sLines = File.ReadAllLines(cboPartProgram.Text)
                 .Where(s => !s.StartsWith("'"))
                 .Select(s => new
                       {
                           SValue = Regex.Match(s, "(?<=S)[\\d.]*").Value,
                       })
                 .ToArray();

string LastSValue = "";
string Value = "";

for (int i = 0; i < sLines.Count(); i++)
{
     if (sLines[i].SValue == "")
     {
         LastSValue = "***";
         Value = LastSValue;
     }
     else
     {
         Value = (sLines[i].SValue);
     }
}

Ok I think I got it.

        for (int i = 0; i < sLines.Length; i++)
            {

            if (sLines[i].SValue == "" && i > 0)
            {
                foreach (var empt in sLines[i].SValue)
                {
                    LastSValue = sLines[i - 1].SValue;
                    Value = LastSValue;
                }
            }
            else
            {
                Value = (sLines[i].SValue);
            }

On a side note, when I copy my code I use the code option above to format it, but I notice someone always has to correct my spacing. Its copied straight from the IDE but there are always spaced each line that I guess shouldn't be. Is there a different way I should do it?

UPDATE

If I should ask this as a new question let me know, but it's so dependent on this that I thought I should keep it here.

Using the code I posted above that does what I needed it too. I've been trying to edit this so that if there is NO previous number, so for example if there if line 1 has no number but the rest do, then just apply the string "NA" otherwise still do what the code above does to the rest of the lines.

I guess maybe the best way would be to just take the results from the above code, and if there are any empty spaces left, apply "NA" but I can't figure it out.

4
  • You REALLY shouldn't have a foreach there. What you're doing is going through individual characters in the string, storing them in empt and doing nothing with it - only repeating the same thing over and over for the number of characters in the string, or possibly zero times if the string is empty. EDIT: actually it is empty, so the code inside never executes. Commented Dec 21, 2016 at 17:43
  • @Neme Can you help me fix that? I"m really gotten myself all confused here to be 100% honest with you. Commented Dec 21, 2016 at 18:48
  • @Neme All I need to do is, if its blank, use the previous number until you reach the next number. Once finished for each tine and there are still any blanks, such as the first line where their would be no previous number, just fill it with "NA". Commented Dec 21, 2016 at 18:49
  • Yea I can't make it work without the foreach line for whatever reason without it only applies the last number to the first blank for each previous number. But ones like the 1st line that have no previous number I can't get those showing "NA". Not sure what's wrong. Commented Dec 21, 2016 at 21:51

3 Answers 3

1

In your example, you just need to take the value of the row before to fill the current value. Something like the following :

for (int i = 0; i < sLines.Length; i++)
{
     if (sLines[i].SValue == "" && i > 0)
     {
         sLines[i].SValue = sLines[i-1].SValue;
     }
     else
     {
         sLines[i].SValue = sLines[i].SValue;
     }
}
Sign up to request clarification or add additional context in comments.

7 Comments

This works for the first blank line only. So if it goes 3,3,blank,blank,5 then this code makes it 3,3,3,blank,5
you are right, I updated my answer. Obviously, the idea is to modify the current collection not a single variable.
Hmm maybe I'm doing something wrong? I'm getting the same result.
Is it still LastSValue = sLines[i-1].SValue; Not sure how to apply your edit
could you take a look at the update I posted(asked) thanks a lot.
|
0

Your example has one more issue but currently I'll focus only on gathering the "last non empty" string.

If you look at your example you can spot few things that could potentially help you finding solution. These are for loop and reference to original list that stays intact.

For my example I'll use Linq because it will be much easier.

First of all I'll copy all from before for loop ( if that makes sense :D ) :

var sLines = File.ReadAllLines(cboPartProgram.Text)
     .Where(s => !s.StartsWith("'"))
     .Select(s => new
     {
         SValue = Regex.Match(s, "(?<=S)[\\d.]*").Value,
     })
     .ToArray();

string LastSValue = "";
string Value = "";

Just because it's okay and will work for now.

With your for loop I'll make modifications :

for (int i = 0; i < sLines.Count(); i++)
{
    // `i` is representing current "index" of processed "word"
    // we can use this to find last "valid" element
    // string notEmpty = sLines.Take(i).LastOrDefault(word => !string.IsNullOrEmpty(word));
    // but since you want to assign this to `Value` and there can be not empty string at `i` index
    // we can make it in one line :
    Value = string.IsNullOrEmpty(sLines[i]) ? sLines.Take(i).LastOrDefault(word => !string.IsNullOrEmpty(word)) : sLines[i].SValue;
    // instead of your previous logic :
    //if (sLines[i].SValue == "")
    //{
    //    LastSValue = "***";
    //    Value = LastSValue;
    //}
    //else
    //{
    //    Value = (sLines[i].SValue);
    //}
 }

Another problem which I think you'll face is that first value ( judging by the input ) can also be empty. Which will throw exception in my example. This will also be impossible to fit this kind of solution because there's no previous value ( at all ).

1 Comment

Still trying to test this one, but it keeps breaking all my code.
0

From what I understand, if you want to store the result in Value and do something else with it inside the loop (instead of changing it in the array), what you probably want is this:

for (int i = 0; i < sLines.Count(); i++)
{
    if (sLines[i].SValue == "")
    {
        Value = LastSValue;
    }
    else
    {
        Value = (sLines[i].SValue);
        LastSValue = Value;
    }

    // use Value
}

I would also suggest using sLines.Length instead of Count(), which is made for sequences where the length isn't known in advance - it's supposed to literally count the elements one by one. In this case it would probably be optimized but if you know you're dealing with an array, it's a good idea to ask for the length directly.

EDIT:

To get "NA" if there's no previous number, just initialize LastSValue to this value before the loop:

string LastSValue = "NA";

That way, if Value is empty and there was not LastSValue set before, it will still be "NA".

EDIT2: A solution similar to the one from @Cubi, to change it in place:

for (int i = 0; i < sLines.Length; i++)
{
     if (sLines[i].SValue == "")
         sLines[i].SValue = i > 0 ? sLines[i-1].SValue : "NA";
}

2 Comments

This doesn't do anything for me. Aside from not doing the "NA" it doesn't fill in the blank lines w/ the previous line's number.
I wasn't sure what you were trying to do because your code sample wasn't complete at first, but if you want to change it in place, simply assign it again - instead of the comment, put sLines[i].SValue = value. But in that case, the other solution from @Cubi would probably be better.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.