Author Topic: Calculation/Conversion with Text Files  (Read 9736 times)

0 Members and 1 Guest are viewing this topic.

ziko30

  • Newt
  • Posts: 51
Calculation/Conversion with Text Files
« on: March 18, 2010, 02:28:50 PM »
I want to do something like this:
Line1TextFile1 x Line1TextFile2 = Line1TextFile3 (where x is not multiplication but a formula) and then repeat with line 2 etc. I would like pointers and not complete solutions for now. Question is am I on the right track doing it this way and if not where did I screw up?

I get this on the command line ( bad argument type: stringp 2.49887 :error#2*Cancel* )  which has to do with data types But I don't know how to fix it.


Code: [Select]
(defun c:Conv (/ M_Open A_Open M_Read A_Read Converted M_Line1
      A_Line1 Convert)

  (setq M_Open  (getfiled "Select M_File" "" "txt" 16)
M_Read  (open M_Open "r")
A_Open  (getfiled "Select A_File" "" "txt" 16)
A_Read  (open M_Open "r")
Converted (open (strcat M_Open "Converted.txt") "w")
  )

  (while (setq M_Line1 (atof (read-line M_Read)))
    (setq A_Line1 (atof (read-line A_Read)))
    (setq Convert (atof (* 100 (/ A_Line1 (- 100 M_Line1)))))
    (write-line Convert Converted)
  )
  (close Converted)
  (close M_Open)
  (close A_Open)
  (princ)
)
« Last Edit: March 18, 2010, 03:43:08 PM by ziko30 »
============== Acad Map 3D 2009================

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Calculation/Conversion with Text Files
« Reply #1 on: March 18, 2010, 02:53:28 PM »
Ziko,

You want pointers, so I shan't rewrite the code, but here are a few things to consider:

  • Allow for the user not selecting a file to use, it can be as simple as an IF statement.
  • (atof nil) will return an error, so check that the read-line returns true before trying to convert it to a float
  • (atof (* 100 (/ A_Line1 (- 100 M_Line1)))) I'm not sure why you are trying to convert your calculations (float type) from a string type to a float type using atof, better the other way around. Look into the rtos function.

That should get you more on the right tracks,

Lee

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Calculation/Conversion with Text Files
« Reply #2 on: March 18, 2010, 03:13:05 PM »
You might also want to read in each file to a list before you process them.  Check to see if they are the same length.  I would only have one file open at a time, as I think that would speed up the process also.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Calculation/Conversion with Text Files
« Reply #3 on: March 18, 2010, 05:16:17 PM »
Yes, that way you can use a subroutine to open/read/close the file & return the list of data.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

ziko30

  • Newt
  • Posts: 51
Re: Calculation/Conversion with Text Files
« Reply #4 on: March 18, 2010, 11:28:51 PM »
Thanks all for the advice.
Taking this little by little working with one file first;  I am able to read a file to a list and write the list to another file. Except it is an infinity loop and it keeps writing to the file. Why is the while loop not terminating? The errors/misconceptions might make you cringe... but I would like for them to be pointed out. I have also added two sample txt files I am using for testing. Eventually I will check length and reverse lists before processing.
Code: [Select]
(Defun C:Conv2 (/ M_Open M_Read M_Line1 M_line2 Conv)

  (if (setq M_Open (getfiled "Select M_File" "" "txt" 16))
    (setq M_Read (open M_Open "r"))
    (prompt "\nUnable to open file.")
    )
    
    (while (Setq M_Line1 (read-line M_Read))
      (Setq Listfile1 (cons M_Line1 Listfile))
      )
      
    (Close M_Read)

    (setq conv (open "C:\\Projects\\Converted.txt" "w"))

    (while (setq M_Line2 (car Listfile1))
      (write-line M_Line2 conv))
    (close conv)
    
  )
« Last Edit: March 18, 2010, 11:32:24 PM by ziko30 »
============== Acad Map 3D 2009================

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Calculation/Conversion with Text Files
« Reply #5 on: March 18, 2010, 11:37:34 PM »
you're missing a --

(setq listFile1 (cdr Listfile1))

can you figure out where it should go?
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Calculation/Conversion with Text Files
« Reply #6 on: March 19, 2010, 02:01:14 AM »
you're missing a --

(setq listFile1 (cdr Listfile1))

can you figure out where it should go?


Now, that's my style of help   :wink:
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Calculation/Conversion with Text Files
« Reply #7 on: March 19, 2010, 06:59:47 AM »
Teach a man to fish and all that...  :roll:

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Calculation/Conversion with Text Files
« Reply #8 on: March 19, 2010, 08:15:47 AM »
Rolling eyes? wtf, I did something wrong?
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Calculation/Conversion with Text Files
« Reply #9 on: March 19, 2010, 08:37:03 AM »
Rolling eyes? wtf, I did something wrong?


Nope, not as far as I'm concerned.
I like to see people encouraged to help themselves.

.... thought you knew that .. sorry for the confusion.  :|
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

ziko30

  • Newt
  • Posts: 51
Re: Calculation/Conversion with Text Files
« Reply #10 on: March 19, 2010, 08:58:41 AM »
Rolling eyes? wtf, I did something wrong?

No you are ok...Its the help I asked for and at this point I will eat humble pie and say stick it in there for me if you would. I think it should go in the last while block so that my list is reduced by an element with every iteration. I have tried putting it where I think it should but no success. 
============== Acad Map 3D 2009================

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Calculation/Conversion with Text Files
« Reply #11 on: March 19, 2010, 09:19:53 AM »
Nope, not as far as I'm concerned.
I like to see people encouraged to help themselves.

.... thought you knew that .. sorry for the confusion.  :|

I understood your comments, and generally pretend to know your bent on teaching vs hand outs, I just didn't get the rolling eyes.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Calculation/Conversion with Text Files
« Reply #12 on: March 19, 2010, 09:26:26 AM »
No you are ok...Its the help I asked for and at this point I will eat humble pie and say stick it in there for me if you would. I think it should go in the last while block so that my list is reduced by an element with every iteration. I have tried putting it where I think it should but no success.

... an infinity loop -- keeps writing to the file. Why is the while loop not terminating? ...

Based solely on that ^ comment I assume the problem is in the "write" loop:

Code: [Select]
    (while (setq M_Line2 (car Listfile1))
      (write-line M_Line2 conv)
    )

Looking at it, the exit condition is "when (setq M_Line2 (car Listfile1)) returns nil". Examining said loop, no code modifies Listfile1, therefore (setq M_Line2 (car Listfile1)) will always be non nil, ergo infinite loop. Clear as mud? PS: It's the only part of the proggy I looked at, other errors may abound.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ziko30

  • Newt
  • Posts: 51
Re: Calculation/Conversion with Text Files
« Reply #13 on: March 19, 2010, 09:26:31 AM »
Teach a man to fish and all that...  :roll:

Lee FYI,

Eventually, I want to skip the first 6 lines of TextFile1 and TextFile2 but copy the 6 lines to TextFile3 (The first 6 lines should have same values for all 3 files and not to be processed).  All 3 files will have the same total number of lines. I will also want the routine to read file 1 and 2 till it encounters a string or blank then stop.  Last  time I posted something I simply used your code, but  Iam hoping I  can work my  way through this one with swampers. Failing that I will ask for a complete solution.  :oops:
============== Acad Map 3D 2009================

ziko30

  • Newt
  • Posts: 51
Re: Calculation/Conversion with Text Files
« Reply #14 on: March 19, 2010, 09:36:44 AM »
No you are ok...Its the help I asked for and at this point I will eat humble pie and say stick it in there for me if you would. I think it should go in the last while block so that my list is reduced by an element with every iteration. I have tried putting it where I think it should but no success.

... an infinity loop -- keeps writing to the file. Why is the while loop not terminating? ...

Based solely on that ^ comment I assume the problem is in the "write" loop:

Code: [Select]
    (while (setq M_Line2 (car Listfile1))
      (write-line M_Line2 conv)
    )

Looking at it, the exit condition is "when (setq M_Line2 (car Listfile1)) returns nil". Examining said loop, no code modifies Listfile1, therefore (setq M_Line2 (car Listfile1)) will always be non nil, ergo infinite loop. Clear as mud? PS: It's the only part of the proggy I looked at, other errors may abound.

Ok thanks... I am moving along. Here is how I have it and it worked.
Code: [Select]
(while (setq M_Line2 (car Listfile))
      (write-line M_Line2 conv) (setq listFile (cdr Listfile))
    )
============== Acad Map 3D 2009================