0

what i'm trying to do here is to delete the longest line from a txt file. Code does it's job, but i also need it to delete multiple "longest lines" and blank lines as well. Any ideas on how to do it?

Code is in C#

    namespace _5_2
    {
    //------------------------------------------------------------
    class Program
    {
         const string CFd = "..\\..\\U1.txt";
         const string CFr = "..\\..\\Results.txt";
         static void Main(string[] args)
         {
             int nr;
             Read(CFd, out nr);
             Print(CFd, CFr, nr);
             Console.WriteLine("Longest line nr. {0, 4:d}", nr + 1);
             Console.WriteLine("Program done");
         }
         //------------------------------------------------------------
         /** Finds number of the longest line.
         @param fv - text file name
         @param nr - number of the longest line */
         //------------------------------------------------------------
         static void Read(string fv, out int nr)
         {
             string[] lines = File.ReadAllLines(fv, Encoding.GetEncoding(1257));
             int ilgis = 0;
             nr = 0;
             int nreil = 0;
         foreach (string line in lines)
         {
            if (line.Length > ilgis)
               {
                  ilgis = line.Length;
                   nr = nreil;
               }
              nreil++;
          }
        }
         static void Print(string fv, string fvr, int nr)
         {
             string[] lines = File.ReadAllLines(fv, Encoding.GetEncoding(1257));
             int nreil = 0;
             using (var fr = File.CreateText(fvr))
             {
                 foreach (string line in lines)
                 {
                     if (nr != nreil)
                     {
                         fr.WriteLine(line);
                     }
                     nreil++;
                 }
             }
         }
      }
  }
Ron Beyer
  • 10,758
  • 1
  • 18
  • 35
Deividas
  • 79
  • 1
  • 8
  • 1
    You could identify the longest line, and then loop through the list, deleting all of that length. To also delete empty ones, you could test against String.IsNullOrWhiteSpace. – B. Clay Shannon-B. Crow Raven Nov 11 '15 at 19:05
  • I deleted my answer in favor of @B.ClayShannon's superior one. Aren't you going to post that? – André Chalella Nov 11 '15 at 19:14
  • I'm going to try that looping though the list to delete multiples, but i still don't understand what to do with the empty ones. In my Print method i changed line if ( nr != nreil) to if (line != "") . This works until it's just a blank line, but as soon as i "write" a space in that line, line stays. – Deividas Nov 11 '15 at 19:19
  • If you add a space, you've added a line. Just because you can't see air, that doesn't mean it's not there / has no weight. – B. Clay Shannon-B. Crow Raven Nov 11 '15 at 19:45

3 Answers3

0

I would suggest using LINQ. Take advantage of the .Max extension method and iterate over the string array.

string[] lines = { "1", "two", "three" };
var longestLine = lines.Max(line => line.Length);
var revised = lines.Where(line => line.Length < longestLine).ToArray();

The revised variable will contain a string array that excludes the lines with the longest line count.

David Pine
  • 22,888
  • 9
  • 75
  • 103
0

Read lines, filter out empty lines and the 10 longest lines, write lines:

     string[] lines = File.ReadAllLines(inputFile, Encoding.GetEncoding(1257));
     var filtered = lines
         .Where(line => line.Length > 0) // remove all empty lines
         .Except(lines.OrderByDescending(line => line.Length).Take(10)); // remove 10 longest lines
     File.WriteAllLines(outputFile, filtered);
Mud
  • 26,791
  • 11
  • 57
  • 87
-1

You could identify the longest line, and then loop through the list, deleting all of that length. To also delete empty ones, you could test against String.IsNullOrWhiteSpace.

Something like (pseudocode):

     foreach (string line in lines)
     {
        if (String.IsNullOrWhiteSpace(line)) 
        {
            lines.Delete(line);
            Continue;
        }
        if (line.Length >= longestLine) // ">=" instead of "==" just to be on the safe side
        {
           lines.Delete(line);
        }
    }
B. Clay Shannon-B. Crow Raven
  • 5,261
  • 133
  • 434
  • 811
  • >= to be on safer side?? when u know 1+1 = 2. will u say it is " >=2" you think when u loop through or use linq to find the longest then that time some miracle can happen?? and even if i consider all the point still if you say >= will mean you are not removing only longest .. – Deepak Sharma Nov 11 '15 at 19:24
  • Yes, I know of nothing but miracles (apologies to Walt Whitman). – B. Clay Shannon-B. Crow Raven Nov 11 '15 at 19:26
  • just sharing my views that's it.. completely not agree with `>=` it should be and must be `==` if u use >= mean u are not deleting longest.. you are actually deleting multiple variable length items. >= 10 mean length of 10,11,12 and so on.. mean not deleting longest. so all flop.. – Deepak Sharma Nov 11 '15 at 19:29
  • If there's an 11 or 12 in your scenario, then 10 is not the longest; and the point is to delete the longest. – B. Clay Shannon-B. Crow Raven Nov 11 '15 at 19:30
  • hahaha.. you catch it.. when 10 is not the longest for 11,12 then whats the use to put this `>` ?? – Deepak Sharma Nov 11 '15 at 19:32
  • 1
    may be.. and can I modify the collection under `foreach`?? do we have Delete method on array?? where is that located?? – Deepak Sharma Nov 11 '15 at 19:43
  • @DeepakSharma No you cannot remove items from an array. If you want to remove items you should use a `List` instead. And no you wouldn't be able to update the list inside of a `foreach`. Instead you'd need to do a `for` loop that starts at the end and decrements to the beginning. – juharr Nov 11 '15 at 19:50
  • Lines.Delete thingy didn't worked for me too. But i found a way. : If (!String.IsNullOrWhiteSpace(line)) and then just simple fr.Writeline(line) – Deividas Nov 11 '15 at 19:51
  • @juharr never mind, i know we dont have any delete method. i was asking to Shannon. using for foreach you can not modify the collection either its list array or enumerable. I 'm pretty clear in it. we have to use the For loop – Deepak Sharma Nov 11 '15 at 19:53
  • @Deividas you must have checked it before accepting it as answer/.. :P – Deepak Sharma Nov 11 '15 at 19:54
  • @B.ClayShannon Yes I noticed it later. But I am really surprised this is code or pseudo?? :P looks like code but titled as pseudo.. – Deepak Sharma Nov 11 '15 at 19:57
  • @B.ClayShannon Pseudocode is somewhere between a written description and actual code. The problem here is that this looks a lot more like actual code than a written description. – juharr Nov 11 '15 at 19:57