www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - requesting a few pointers

reply a.c.edwards <a.c.edwards_member pathlink.com> writes:
Gentlemen,
By share luck and a whole lot of trial and error, I've hacked a small utility
program to reconcile a few reports. It's not very effective but it gets the job
done. I'm wondering if you could look at it and tell me where I've gone wrong
and if possible how I could do things better.
Thanks in advance,
Andrew

-----code-----
# import std.file;
# import std.stdio;
# import std.stream;
# import std.string;
# 
# extern(C) int system(char*);
# 
# static char[][] files = [
#   "94410_TAM(Unit).txt", "94411_TAM(Unit).txt",
#   "94420_TAM(Unit).txt", "94430_TAM(Unit).txt",
# ];
# 
# void remove(char[] file)
# {
#   if(exists(file))
#     std.file.remove(file);
# }
# 
# uint _in(char[][] str, char[] val)
# {
#   foreach(int ndx, char[] s; str)
#     if(val == s)
#       return ndx;
#   return -1;
# }
# 
# int main()
# {
#  
#   uint dl;
#   char[] lastTam;
#   char[] unit;
#   
#   char[] dir = getcwd();
# 
#   remove("opendocs.txt");
#   remove("secreps.txt");
# 
#   chdir(r"\dpr");
#   system("parse");
#   system("splitdpr");
#   system("move opendocs.txt " ~ dir);
#   system("move secreps.txt " ~ dir);
#   chdir(dir);
#   
#   char[][] opendocs = splitlines(cast(char[])read("opendocs.txt"));         
#   char[] lastFile;
#   bool over = false;
#   foreach(int n, char[] file; files) {
#   
#     unit = file[0 .. 5] ~ ".rtf";
#     remove(unit);
#     append(unit, open);
#     if(exists(file))
#     {
#       if(exists(file[0 .. 5] ~ "dl.txt"))
#         remove(file[0 .. 5] ~ "dl.txt");
#       char[][] tam = splitlines(cast(char[])read(file));
#       
#       bool newfile = true;
#       foreach(int ndx, char[] line; tam) {
#        
#         if (_in(mares, line[2 .. 7]) != -1 &&
#             (line[65] == 'M' || line[65] == 'C' ||
#             (line[60..62] == "05" && line[65] == 'N') ||
#             (line[60..62] == "02" && line[65] == 'N')))
#         {
#           if (line[2..7] != lastTam) {
#             append(unit, `\par ` ~ "\n");
#             if (newfile) {
#               newfile = false;
#               last:
#               if( dl > 0)
#                 append (lastFile ~ "dl.txt", lastTam ~ "    " ~
rjustify(toString(dl), 3) ~ "\r\n");
#             }
#             else
#             {
#               if(dl > 0)
#                 append (file[0 .. 5] ~ "dl.txt", lastTam ~ "    " ~
rjustify(toString(dl), 3) ~ "\r\n");
#               dl = 0;           
#               lastTam = "";
#             }
#           }
#           if(line[65] == 'M' && line[69] == '2')
#             dl++;
#           append(unit, `\par ` ~ line ~ "\n");
#           lastTam = line[2 .. 7];
#           documents(unit, opendocs, line[128 .. 133]);
#           if (n == files.length - 1 && over)
#             goto end;
#         }
#         else if (_in(mares, line[2 .. 7]) != -1 &&
#             (line[65] == 'M' || line[65] == 'C' ||
#             (line[60..62] == "05" && line[65] == 'N') ||
#             (line[60..62] == "02" && line[65] == 'N')))
#         {
#           if (line[2..7] != lastTam)
#             append(unit, `\par ` ~ "\n");
#           append(unit, `\par ` ~ line ~ "\n");
#           lastTam = line[2 .. 7];
#           documents(unit, opendocs, line[128 .. 133]);
#         }
#       }
#       if (n == files.length - 1)
#       {
#         lastFile = file[0 .. 5];
#         over = true;
#         goto last;
#       }
#     }
#     end:
#     append(unit, close);
#     lastFile = file[0 .. 5];
#   }
#   return 0;
# }
# 
# void documents(char[] file, char[][] opendocs, char[] ero)
# {
#   char[] info;
#   info.length = 51;
#   info[] = ' ';
# 
#   foreach(char[] doc; opendocs)
#     if(doc[0 .. 5] == ero)
#     {
#       append(file, `\par ` ~ info ~ doc[6 .. length] ~ "\n");
#     }
# }
# 
# static char[][] mares = [
# "A0000", "A0001", "A0002",
# "B0000", "B0001", "B0002",
# "C0000", "C0001", "C0002",
# "D0000", "D0001", "D0002",
# "E0000", "E0001", "E0002",
# "F0000", "F0001", "F0002",
# ];
Oct 06 2005
next sibling parent reply Chris Sauls <ibisbasenji gmail.com> writes:
a.c.edwards wrote:
 Gentlemen,
 By share luck and a whole lot of trial and error, I've hacked a small utility
 program to reconcile a few reports. It's not very effective but it gets the job
 done. I'm wondering if you could look at it and tell me where I've gone wrong
 and if possible how I could do things better.
 Thanks in advance,
 Andrew

One thing that immediately comes to mind. You can replace this:
 # extern(C) int system(char*);

With this: # import std.process; Which already defines a D-linkage system() wrap.
 #       if(exists(file[0 .. 5] ~ "dl.txt"))
 #         remove(file[0 .. 5] ~ "dl.txt");

custom remove() overload earlier that inserts the if(exists()) expression itself. One other thing that's immediate, is that I'm curious as to why you import std.stream and then never use it, at least not that I noticed? -- Chris Sauls
Oct 10 2005
parent a.c.edwards <a.c.edwards_member pathlink.com> writes:
In article <dieg7j$m8j$1 digitaldaemon.com>, Chris Sauls says...
a.c.edwards wrote:
 Gentlemen,
 By share luck and a whole lot of trial and error, I've hacked a small utility
 program to reconcile a few reports. It's not very effective but it gets the job
 done. I'm wondering if you could look at it and tell me where I've gone wrong
 and if possible how I could do things better.
 Thanks in advance,
 Andrew

One thing that immediately comes to mind. You can replace this:
 # extern(C) int system(char*);

With this: # import std.process;

Thanks, I didn't realize that it was already wrapped in std.process. I've made the change.
Which already defines a D-linkage system() wrap.

 #       if(exists(file[0 .. 5] ~ "dl.txt"))
 #         remove(file[0 .. 5] ~ "dl.txt");

custom remove() overload earlier that inserts the if(exists()) expression itself.

Got it. I through that function in after I realized I was using that statement alot.
One other thing that's immediate, is that I'm curious as to why you import
std.stream and 
then never use it, at least not that I noticed?

I was trying to imporove the program and realized it was a whole lot faster to write to a file stream than to continuously append to the file. I will be using it shortly. Thanks for your time.
-- Chris Sauls

Oct 15 2005
prev sibling parent reply BCS <BCS_member pathlink.com> writes:
#1, Not enough comments!!
Oct 10 2005
parent a.c.edwards <a.c.edwards_member pathlink.com> writes:
In article <dif09n$4dq$1 digitaldaemon.com>, BCS says...
#1, Not enough comments!!

Point taken. I guess that is one of the problems all neophytes face. I will get better, I'm sure, but I was looking for something to speed up a process and comments were the last thing on my mind. Thanks, Andrew
Oct 15 2005