• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Would this be a difficult program to write? Details inside

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.
did this while on a con call. 20-25 lines, depending on how you count them

NOT TESTED
Code:
using System;
using System.Collections.Generic;
using System.IO;

namespace Cad_Converter
{
    class Program
    {
        static void Main(string[] args)
        {
            // the source file path
            string sourceFile = args[0];
        
            // destination file path
            string destFile = args[1];
        
            // List of started "codes"
            var codes = new List<string>();
        
            string previousCode = "";
        
            foreach (var s in File.ReadAllLines(sourceFile))
            {
                var code = ExtractCode(s);
                if (code == previousCode)
                {
                    File.AppendAllText(destFile, s + nl);
                }
                else
                {
                    var txt = "END" + nl;
                    if (codes.Contains(code))
                    {
                        txt += "CONT ";
                    }
                    else
                    {
                        txt += "BEG ";
                        codes.Add(code);
                    }
                    txt += code + nl + s + nl;
                    File.AppendAllText(destFile, txt);
                    previousCode = code;
                }
            }
        
            // Add another end at the end
            File.AppendAllText(destFile, "END");
        }
        
        private static string nl = Environment.NewLine;

        static string ExtractCode(string line)
        {
            // Grab what is between the quotes
            int start = line.IndexOf('"')+1;
            int len = line.Length - start - 1;
            return line.Substring(start, len);
        }
    }
}
 
got bored while waiting for a test suite to run

Bonus points for the first person to name the language... it should be very obvious.

Code:
tracked = Hash.new {|h,k| h[k] = false}
tracking = nil

while gets
  line = $_.split

  if line.last =~ /^[A-Z]+\"/
    puts "END" if tracked[tracking]
    tracking = line.last.tr('"','')
    tracked[tracking] = true
    puts "BEG #{tracking}"
    puts line.slice(0..-3).join(" ") + " \"#{tracking}\""
    next
  end

  if line.last !~ /^\"#{tracking}\"$/ && tracking != nil
    puts "END"
    tracking = line.last.tr('"','')
    if tracked[tracking]
      puts "CONT #{tracking}"
    end
  end

  puts $_
end

puts "END" if tracked[tracking]
 
Last edited:
did this while on a con call. 20-25 lines, depending on how you count them

NOT TESTED
Code:
using System;
using System.Collections.Generic;
using System.IO;

namespace Cad_Converter
{
    class Program
    {
        static void Main(string[] args)
        {
            // the source file path
            string sourceFile = args[0];
        
            // destination file path
            string destFile = args[1];
        
            // List of started "codes"
            var codes = new List<string>();
        
            string previousCode = "";
        
            foreach (var s in File.ReadAllLines(sourceFile))
            {
                var code = ExtractCode(s);
                if (code == previousCode)
                {
                    File.AppendAllText(destFile, s + nl);
                }
                else
                {
                    var txt = "END" + nl;
                    if (codes.Contains(code))
                    {
                        txt += "CONT ";
                    }
                    else
                    {
                        txt += "BEG ";
                        codes.Add(code);
                    }
                    txt += code + nl + s + nl;
                    File.AppendAllText(destFile, txt);
                    previousCode = code;
                }
            }
        
            // Add another end at the end
            File.AppendAllText(destFile, "END");
        }
        
        private static string nl = Environment.NewLine;

        static string ExtractCode(string line)
        {
            // Grab what is between the quotes
            int start = line.IndexOf('"')+1;
            int len = line.Length - start - 1;
            return line.Substring(start, len);
        }
    }
}

So your code doesn't solve the problem but it was nice enough for you to write some code to get him going.

I took your code and wrote it in python so he can see what it looks like:

Code:
import csv

class CadConverter:
    
    def run(self, src_file, dst_file):
        codes = []
        new_lines = []
        with open(src_file, 'rb') as csvfile:
            lines = csv.reader(csvfile, delimiter='\n', quoting=csv.QUOTE_NONE)
            previousCode = None
            for row in lines:
                code = row[0].split()[-1].replace('"', '') # extract code
                if code == previousCode:
                    new_lines.append(row[0])
                else:
                    new_lines.append("END")
                    if code in codes:
                        new_lines.append("CONT " + code)
                    else:
                        new_lines.append("BEG " + code)
                        codes.append(code)
                    new_lines.append(row[0])
                    previousCode = code
            new_lines.append("END")
        with open(dst_file, 'w') as f:
            for line in new_lines:
                f.write(line + '\n')
    
def main():
    cadConverter = CadConverter()
    cadConverter.run('cadFile.txt', 'output.txt')

if __name__ == "__main__":
    main()
 
Last edited:
got bored while waiting for a test suite to run

Bonus points for the first person to name the language... it should be very obvious.

Code:
tracked = Hash.new {|h,k| h[k] = false}
tracking = nil

while gets
  line = $_.split

  if line.last =~ /^[A-Z]+\"/
    puts "END" if tracked[tracking]
    tracking = line.last.tr('"','')
    tracked[tracking] = true
    puts "BEG #{tracking}"
    puts line.slice(0..-3).join(" ") + " \"#{tracking}\""
    next
  end

  if line.last =~ /^\"#{tracking}\"$/
    puts $_
    next
  elsif tracking != nil
    puts "END"
    tracking = line.last.tr('"','')
    if tracked[tracking]
      puts "CONT #{tracking}"
    end
  end

  puts $_
end

puts "END" if tracked[tracking]
that would be ruby.
 
How so?

Only problem I see is it will add a BEG DCB line at the beginning, whereas his sample output does not. Easy edge case to fix.

This is what your program outputs:

Code:
END
BEG DCB
NEZ 13000 4860271.330 683259.150 92.391 "DCB"

END
BEG B GO
NEZ 13001 4860271.057 683258.136 92.418 "B GO"

END
BEG GO
NEZ 13002 4860260.096 683260.271 92.683 "GO"

NEZ 13003 4860293.237 683255.697 92.738 "GO"

NEZ 13004 4860318.665 683253.699 92.434 "GO"

END
BEG CB
NEZ 13005 4860319.888 683255.541 92.423 "CB"

END
BEG B EP
NEZ 13006 4860327.588 683250.237 92.557 "B EP"

END
BEG B BD
NEZ 13007 4860332.990 683248.822 91.965 "B BD"

END
BEG BD
NEZ 13008 4860338.890 683270.007 91.919 "BD"

END
BEG EP
NEZ 13009 4860333.877 683272.528 92.534 "EP"

END
CONT GO
NEZ 13010 4860324.544 683274.007 92.337 "GO"

NEZ 13011 4860296.177 683276.034 92.416 "GO"

NEZ 13012 4860272.743 683277.075 92.369 "GO"

END
CONT B EP
NEZ 13013 4860263.201 683277.590 92.347 "B EP"

END
CONT B GO
NEZ 13014 4860260.877 683277.765 92.429 "B GO"

END
CONT GO
NEZ 13015 4860262.766 683301.026 92.175 "GO"

END
CONT EP
NEZ 13016 4860264.814 683300.366 92.038 "EP"

END
BEG GO"
NEZ 13017 4860274.145 683299.075 92.101 "GO" 

END

code.png
 
this gets you close, just need to delete the first line of the output.

f2BDCDf.png


Code has gotten a little messy, could probably refactor that down and make it more readable.
 
Well you switched my AppendAllText to a A WriteLine, which of course adds an extra line break.

Console.Write would work better.

Uhhh..... No, I used Console.Out.WriteLine. Did you look at the screen shot?

And its more then just a line break that is different with his output. Did you compare against his output?

Your keeping "B Go" etc...
 
Uhhh..... No, I used Console.Out.WriteLine. Did you look at the screen shot?
I got that, the point being one adds a newline at the end, the other does not.

And its more then just a line break that is different with his output. Did you compare against his output?

Your keeping "B Go" etc...
I didn't see that rule at first, my last example above handles it.
 
I got that, the point being one adds a newline at the end, the other does not.

I didn't see that rule at first, my last example above handles it.

it should only add BEG ... above where the line had "B ..." instead of adding a BEG ... if it hasn't seen the code yet.

also the lines that have "B ..." should just be "..."

So "B GO" would be "GO" etc...

Also there is a trailing " on one of the lines at the end: BEG GO"
 
Last edited:
To get some practice my rusty C# skills, I wrote this up last night if you want another C# variation. Seemed to work fine in my testing. It's a console application. The exe accepts the old filename/path and new filename/path as the first two arguments (i.e. SurveyUtility.exe c:\temp\test.txt c:\temp\test2.txt). If the new file already exists, it'll end up overwriting it. I wasn't sure which you'd prefer, overwriting it, appending to it, or something else.

Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;


namespace SurveyUtility
{
   class ConvertFile
   {
      static void Main(string[] args)
      {
         string oldFilePath = args[0];
         string newFilePath = args[1];

         string line;
         string currentCode;
         string previousCode = String.Empty;

         if (File.Exists(oldFilePath))
         {
            using (StreamReader srOldFile = new StreamReader(oldFilePath))
            {
               using (StreamWriter srNewFile = new StreamWriter(newFilePath, false))
               {
                  line = srOldFile.ReadLine();

                  while (line != null)
                  {
                     currentCode = line.Split(new string[] { "\"" }, StringSplitOptions.None)[1];

                     if (previousCode != currentCode && previousCode != "DCB" && previousCode != "CB" && currentCode != "DCB")
                     {
                        srNewFile.WriteLine("END");
                     }
                    
                     if (currentCode.Substring(0, 2) == "B ")
                     {
                        currentCode = currentCode.Substring(2);
                        srNewFile.WriteLine("BEG " + currentCode);
                        line = line.Replace("\"B ", "\"");
                     }
                     else if (previousCode != currentCode && currentCode != "CB" && currentCode != "DCB")
                     {
                        srNewFile.WriteLine("CONT " + currentCode);
                     }

                     srNewFile.WriteLine(line);

                     previousCode = currentCode;
                     line = srOldFile.ReadLine();

                     if (line == null)
                     {
                        srNewFile.WriteLine("END");
                     }
                  }
               }
            }
         }
      }
   }
}
 
Last edited:
C# without any hard-coded codes like DCB or CB.

To test on compileonline:
Code:
using System;
using System.Collections.Generic;
using System.IO;

namespace ACAD
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourceFile = "input.txt";
            var codes = new List<string>();
            var code = "";
            string previousCode = "";

            foreach (var s in File.ReadAllLines(sourceFile))
            {
                var finalLine = s.Replace("B ", "");
                code = finalLine.Split('"')[1];
                if (!code.Equals(previousCode ))
                {
                    if (codes.Contains(previousCode))
                    {
                        Console.WriteLine("END");
                    }
                    if (!finalLine.Equals(s))
                    {
                        if (!codes.Contains(code))
                        {
                            codes.Add(code);
                        }
                        Console.WriteLine("BEG " + code);
                    }
                    else if (codes.Contains(code))
                    {
                        Console.WriteLine("CONT " + code);
                    }
                    previousCode = code;
                }
                Console.WriteLine(finalLine);
            }
            if (codes.Contains(previousCode ))
            {
                Console.WriteLine("END");
            }
        }
    }
}

To produce an actual file:
Code:
using System;
using System.Collections.Generic;
using System.IO;

namespace ACAD
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourceFile = @"c:temp\input.txt";
            string destFile = @"c:temp\output.txt";
            var codes = new List<string>();
            var code = "";
            var output = new List<string>();
            string previousCode = "";

            foreach (var s in File.ReadAllLines(sourceFile))
            {
                var finalLine = s.Replace("B ", "");
                code = finalLine.Split('"')[1];
                if (!code.Equals(previousCode))
                {
                    if (codes.Contains(previousCode))
                    {
                        output.Add("END");
                    }
                    if (!finalLine.Equals(s))
                    {
                        if (!codes.Contains(code))
                        {
                            codes.Add(code);
                        }
                        output.Add("BEG " + code);
                    }
                    else if (codes.Contains(code))
                    {
                        output.Add("CONT " + code);
                    }
                    previousCode = code;
                }
                output.Add(finalLine);
            }
            if (codes.Contains(previousCode))
            {
                output.Add("END");
            }
            File.WriteAllLines(destFile, output);
        }
    }
}
 
Last edited:
It really is amazing to see all the different ideas and thoughts from different programmers to accomplish the same task. Thanks a bunch guys.
 
What would be the best "easy-to-use" preferably free program to use to write this with....Extreme novice here.

python.

you could have learned the line handling and regex necessary to do the job in less time than it took to do those 6000 lines.
 
Back
Top