1

I have 1 array with many string lines.

For every line in that array, if a condition is met, I have a console app that displays the line from the array, with some modifications (I call this the outputLine).

How do I make an array from the list of outputLines ?

class Program
{
    static void Main(string[] args)
    {
        string[] linesArray = File.ReadAllLines(@"C:\Users\AK1\Desktop\CC2_B.TXT");
        int linecount = File.ReadAllLines(@"C:\Users\AK1\Desktop\CC2_B.TXT").Length;            

        for (int i = 0; i < linecount; i++)
        {
            if (linesArray[i].Contains("2008/12"))
            {
                string[] outputArray = linesArray;

                string outputLine = "yes    " + outputArray[i];
                Console.WriteLine(outputLine);

            }

        }

        Console.ReadLine();
    }
}
sokkyoku
  • 2,161
  • 1
  • 20
  • 22
AjK
  • 11
  • 4
  • Can you add the tag with the language you're using, please? – AgataB Sep 06 '16 at 13:39
  • 1
    Can't you create an array and keep adding the outputLine to it? – Deendayal Garg Sep 06 '16 at 13:39
  • This sounds like a wise good idea. I assume i declare it outside the FOR loop, but how to I keep adding it to the new array ? – AjK Sep 06 '16 at 13:51
  • It actually leads to very poor performance, as you'd have to reallocate space for the array and copy every value over every time you enlarge it by adding a new line *or* allocate a massive array that might contain useless space (which might be better than a List is performance is critical and RAM is cheap/your file isn't too big) – sokkyoku Sep 06 '16 at 13:54
  • Also @AjK `string[] outputArray = linesArray;` is a bit useless. While it won't create a copy of the array (thankfully), why bother creating a new reference to the same array, just use `linesArray` directly. – sokkyoku Sep 06 '16 at 13:57

4 Answers4

1

Rather than an array, which has to define its size at declaration in C#, you're looking for a List<string>. You might want to refer to this answer about adding elements to an array to better understand the difference between List and array

Add using System.Collections.Generic at the top of your file and modify your code as such:

class Program
{
    static void Main(string[] args)
    {
        string[] linesArray = File.ReadAllLines(@"C:\Users\AK1\Desktop\CC2_B.TXT");
        int linecount = File.ReadAllLines(@"C:\Users\AK1\Desktop\CC2_B.TXT").Length;            
        List<string> outputLines = new List<string>();

        for (int i = 0; i < linecount; i++)
        {
            if (linesArray[i].Contains("2008/12"))
            {
                string outputLine = "yes    " + linesArray[i];
                outputLines.Add(outputLine);
                Console.WriteLine(outputLine);
            }

        }
        Console.ReadLine();
    }
}

Then if you really need it to be an array (like to pass it to another function), you can call outputLines.ToArray()

Community
  • 1
  • 1
sokkyoku
  • 2,161
  • 1
  • 20
  • 22
  • Thanks - this is good. So this adds the "outputLine" to the "outputLines" list which is great. But if i change the output from "outputLine" to "outputLines" in the console app - i dont see the list in the console app – AjK Sep 06 '16 at 14:13
  • Not sure what you mean, could you add the new problematic code at the end of your question please? Are you asking how to output the entire `List` after your look rather than outputting it during the loop? If so you could put `outputLines.ForEach(x => Console.WriteLine(x))` just before `Console.ReadLine` but it boils down to basically the same. – sokkyoku Sep 06 '16 at 14:22
  • If your second question is "How do I output a list of strings to the console?" then you could refer to this very in depth answer: http://stackoverflow.com/a/949955/2830652. – sokkyoku Sep 06 '16 at 14:24
  • Also if any of the answers here solve your initial question, could you please click the tick next to it to mark it as the accepted answer and click the upwards arrow next to any answer you find at least helpful? – sokkyoku Sep 06 '16 at 14:25
  • Thanks - i have marked the answers as useful, outputLines.ForEach(x => Console.WriteLine(x)) looks to be doing the trick – AjK Sep 06 '16 at 14:33
  • 1
    Not sure what you did to mark the answers as useful but there seem to be no upvotes on any answers nor an accepted answer. Not going to push on this much further but since you're new to the website I just wanted to tell you what's expected. – sokkyoku Sep 06 '16 at 14:51
1

why not first filter your lines with desired condition by using linq? then you can output in console.

string[] lines = File.ReadAllLines(@"C:\Users\AK1\Desktop\CC2_B.TXT");
string[] filteredLines = lines.Where(line => line.Contains("2008/12")).ToArray();
foreach(string line in filteredLines){
    Console.WriteLine("yes    " + line);
}
Sufian Saory
  • 862
  • 9
  • 14
  • This solution is elegant in terms of written code but requires iterating through the full array once and then through the filtered array once again so if there are many results and performance is a concern it is preferable to go with a good ol' loop – sokkyoku Sep 08 '16 at 16:24
0

Declare an array with a capacity of linecount, in case you need to add every single line. I suppose this is c++? so you can not increase an array size in runtime.

static void Main(string[] args)
{
    int outputCounter=0;
    .......
        if (linesArray[i].Contains("2008/12"))
        {
            string outputLine = "yes    " + outputArray[i];

             outputArray[outputCounter]= outputLine;
             outputCounter++;  
             Console.WriteLine(outputLine);   
        }

    }

Update: For c# just do something like this

static void Main(string[] args)
{
    List<string> outputList = new List<string>();
    .......
        if (linesArray[i].Contains("2008/12"))
        {
            string outputLine = "yes    " + outputArray[i];

             outputList.Add(outputLine);
             Console.WriteLine(outputLine);   
        }

    }
MKougiouris
  • 2,821
  • 1
  • 16
  • 19
0

Try working with Lists instead of arrays if you're wanting to make dynamic additions. For example;

class Program
{
    static void Main(string[] args)
    {
        string[] linesArray = File.ReadAllLines(@"C:\Users\AK1\Desktop\CC2_B.TXT");
        int linecount = File.ReadAllLines(@"C:\Users\AK1\Desktop\CC2_B.TXT").Length;
        // Delcare your list here
        List<string> outputList = new List<string>();            

        for (int i = 0; i < linecount; i++)
        {
            if (linesArray[i].Contains("2008/12"))
            {
                // You can "Add" to lists easily like this
                outputList.Add(linesArray[i]);

                // This will return you the last item in the list
                string outputLine = "yes    " + outputList.Last();
                Console.WriteLine(outputLine);
            }
        }

        // Then if you specifically want it back to an Array
        string[] outputArray = outputList.ToArray();

        Console.ReadLine();
    }
}

Hope this helps :)

George Kerwood
  • 1,248
  • 8
  • 19