Author Topic: Check data ,Return "T" or "nil " I'm in trouble  (Read 5242 times)

0 Members and 1 Guest are viewing this topic.

AIberto

  • Guest
Check data ,Return "T" or "nil " I'm in trouble
« on: July 10, 2015, 09:11:36 AM »
Dear all
Check data ,Return "T" or "nil "  I'm in trouble

(close data_file) Always return "nil"

Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun check_data_file(/  acadpat data_file_patch data_file number_2 rd_row1
  3.                           rd_row3 rd_row4 number_char number_1 )
  4.                                                  
  5. (setq data_file_patch (strcat "d:\\test\\DataFile\\" "data.txt"))
  6. (setq data_file (open data_file_patch "r"))
  7. (if (/= data_file nil)
  8.         (progn
  9.         (setq rd_row1 (open data_file_patch "r"))
  10.         (setq number_char (read-line rd_row1))
  11.         (close rd_row1)
  12.         (if (null(/= number_char "ABCDEF123"));;Compare 1
  13.                 (progn
  14.                 (setq rd_row3 (open data_file_patch "r"))
  15.                 (repeat 3
  16.                 (setq number_1 (read-line rd_row3))
  17.                 )
  18.                 (close rd_row3)
  19.                 (if (null(< (atof "3012415") (atof number_1))) ;;Compare 2
  20.                      (progn
  21.                      (setq rd_row4 (open data_file_patch "r"))
  22.                      (repeat 4
  23.                      (setq number_2 (read-line rd_row4))
  24.                      )
  25.                      (close rd_row4)
  26.                      (if        (null(> (atof "3012415") (atof number_2 ))) ;;Compare 3
  27.                          T
  28.                          nil
  29.                      );;end if                   
  30.                      );;end progn
  31.                          nil
  32.                 );;end if
  33.                 );;end progn
  34.                 nil
  35.         );;end if
  36.         );;end progn
  37.         nil
  38. );;end if
  39. (close data_file)
  40. );;end defun   
  41.  

kpblc

  • Bull Frog
  • Posts: 396
Re: Check data ,Return "T" or "nil " I'm in trouble
« Reply #1 on: July 10, 2015, 09:37:22 AM »
Very strange code. Could you please attach data_file_patch file?
Sorry for my English.

kpblc

  • Bull Frog
  • Posts: 396
Re: Check data ,Return "T" or "nil " I'm in trouble
« Reply #2 on: July 10, 2015, 09:39:30 AM »
I hope this code will works:
Code - Auto/Visual Lisp: [Select]
  1. (defun check_data_file (/ data_file_patch handle str lst)
  2.  
  3.   (if (vl-file-systime (setq data_file_patch (strcat "d:\\test\\DataFile\\" "data.txt")))
  4.     (progn
  5.       (setq handle (open data_file_patch "r"))
  6.       (while (setq str (read-line handle))
  7.         (setq lst (cons str lst))
  8.         ) ;_ end of while
  9.       (close handle)
  10.       (setq lst (reverse lst))
  11.       (and (= (car lst) "ABCDEF123")
  12.            (>= (atof "3012415") (atof (nth 2 lst)))
  13.            (<= (atof "3012415") (atof (nth 3 lst)))
  14.            ) ;_ end of and
  15.       ) ;_ end of progn
  16.     ) ;_ end of if
  17.   ) ;_ end of defun
Sorry for my English.

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Check data ,Return "T" or "nil " I'm in trouble
« Reply #3 on: July 10, 2015, 09:40:07 AM »
Try using this .. then compare the lines in the list.
Code - Auto/Visual Lisp: [Select]
  1. (defun _readfile (filename / of out line)
  2.   (cond   ((and (eq 'str (type filename))
  3.          (setq filename (findfile filename))
  4.          (setq of (open filename "r"))
  5.     )
  6.     (while (setq line (read-line of)) (setq out (cons line out)))
  7.     (close of)
  8.     (reverse out)
  9.    )
  10.   )
  11. )
  12. ;; (_readfile "c:\\test.csv")

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

AIberto

  • Guest
Re: Check data ,Return "T" or "nil " I'm in trouble
« Reply #4 on: July 10, 2015, 09:45:38 AM »
Very strange code. Could you please attach data_file_patch file?

Dear kpblc
Data like this  , yes ,It's Very strange  ,haha. I'm a Newt  .
Code: [Select]
ABCDEF123
0
3012400
3024000


AIberto

  • Guest
Re: Check data ,Return "T" or "nil " I'm in trouble
« Reply #5 on: July 10, 2015, 09:53:01 AM »
I hope this code will works:
Code - Auto/Visual Lisp: [Select]
  1. (defun check_data_file (/ data_file_patch handle str lst)
  2.  
  3.   (if (vl-file-systime (setq data_file_patch (strcat "d:\\test\\DataFile\\" "data.txt")))
  4.     (progn
  5.       (setq handle (open data_file_patch "r"))
  6.       (while (setq str (read-line handle))
  7.         (setq lst (cons str lst))
  8.         ) ;_ end of while
  9.       (close handle)
  10.       (setq lst (reverse lst))
  11.       (and (= (car lst) "ABCDEF123")
  12.            (>= (atof "3012415") (atof (nth 2 lst)))
  13.            (<= (atof "3012415") (atof (nth 3 lst)))
  14.            ) ;_ end of and
  15.       ) ;_ end of progn
  16.     ) ;_ end of if
  17.   ) ;_ end of defun


It looks very useful . I study now ! Thanks kpblc

kpblc

  • Bull Frog
  • Posts: 396
Re: Check data ,Return "T" or "nil " I'm in trouble
« Reply #6 on: July 10, 2015, 09:53:30 AM »
Code at #3 returns T. Is it correct?
Sorry for my English.

AIberto

  • Guest
Re: Check data ,Return "T" or "nil " I'm in trouble
« Reply #7 on: July 10, 2015, 09:54:52 AM »
Try using this .. then compare the lines in the list.
Code - Auto/Visual Lisp: [Select]
  1. (defun _readfile (filename / of out line)
  2.   (cond   ((and (eq 'str (type filename))
  3.          (setq filename (findfile filename))
  4.          (setq of (open filename "r"))
  5.     )
  6.     (while (setq line (read-line of)) (setq out (cons line out)))
  7.     (close of)
  8.     (reverse out)
  9.    )
  10.   )
  11. )
  12. ;; (_readfile "c:\\test.csv")

Thanks ronjonp
a function about get "list "from "file", It's useful .
« Last Edit: July 10, 2015, 08:04:47 PM by AIberto »

AIberto

  • Guest
Re: Check data ,Return "T" or "nil " I'm in trouble
« Reply #8 on: July 10, 2015, 10:06:56 AM »
Code at #3 returns T. Is it correct?


return T , yes ,It's right

a little change

Code - Auto/Visual Lisp: [Select]
  1.  (defun check_data_file (/ data_file_patch )
  2.   (if (vl-file-systime (setq data_file_patch (strcat "e:\\test\\DataFile\\" "data.txt")))
  3.     (progn
  4.       (setq lst (List_FromFile data_file_patch))
  5.       (and (= (car lst) "ABCDEF123")
  6.            (>= (atof "3012415") (atof (nth 2 lst)))
  7.            (<= (atof "3012415") (atof (nth 3 lst)))
  8.            ) ;_ end of and
  9.       ) ;_ end of progn
  10.     ) ;_ end of if
  11.   ) ;_ end of defun
  12.  
  13.  (defun List_FromFile (fn / f l ll)
  14.   (if (setq f (open (findfile fn) "r"))
  15.     (progn
  16.       (while (setq l (read-line f))
  17.         (setq ll (cons  l ll))
  18.       )
  19.       (close f)
  20.     )
  21.   )
  22.   (reverse ll)
  23. )
  24.  
« Last Edit: July 10, 2015, 12:02:21 PM by AIberto »

JohnK

  • Administrator
  • Seagull
  • Posts: 10625
Re: Check data ,Return "T" or "nil " I'm in trouble
« Reply #9 on: July 10, 2015, 10:08:04 AM »
Please don't ask me to do any Lisp code (I havnen't in years and wouldn't be of any use) but if I may make a suggestion:
Read the contents of the file into a list and check the items in the list. The reason being it would be faster if you read contents and operate on a list instead of trying to check as you are reading the file.

I do have these snips of code you can use (left over from my lisping days):

Code - Auto/Visual Lisp: [Select]
  1. (defun file-get-list-from ( name / fp lst read-file-into-list )         ;;/*{{{*/
  2.   ;; file-get-list-from
  3.   ;;
  4.   ;; GENERAL FILE READER
  5.   ;; given a file name this procedure will read the contents
  6.   ;; into a list
  7.   ;;
  8.   ;; EX:
  9.   ;;    (mapcar
  10.   ;;       'read (get-list-from-file
  11.   ;;         (findfile layers-to-load-from-file)))
  12.   ;;
  13.   ;; By: John K
  14.   ;;
  15. (defun read-file-into-list ( str file )         ;;/*{{{*/
  16.   (if str
  17.     (cons
  18.       (if str str)
  19.       (read-file-into-list (read-line file) file))))    ;;/*}}}*/
  20.   (setq fp (open name "R"))
  21.   (setq lst (read-file-into-list (read-line fp) fp))
  22.   (close fp)
  23.  lst )       ;;/*}}}*/
  24.  
Code - Auto/Visual Lisp: [Select]
  1. (defun =| ( a l )     ;;   /*{{{*/
  2.   ;; notequal
  3.   ;;
  4.   ;; preform a equality test on a given object and
  5.   ;; a list of items.
  6.   ;;
  7.   ;; Example: (setq a 1 b 2 c 3 d 4 e 5 f 1)
  8.   ;;          (=| a '(b c d e f))
  9.   ;;          > T
  10.   ;;
  11.   ;;          (=| a '(b c d e))
  12.   ;;          > nil
  13.   ;;
  14.   ;; By: John K
  15.   ;;     01.06.06
  16.   ;;
  17.   ;; Notes:
  18.   ;; If your testing variables you cant just do a ``member ...list''
  19.   ;;
  20.   ;; If your wanting to test variables your going to have to do something
  21.   ;; like this (using the var's i set up in the example)
  22.   ;;
  23.   ;; (and (member (eval a) (mapcar 'eval '(b c d e f))))
  24.   ;;
  25.   ;; Because a simple
  26.   ;; (and (member a '(b c d e f)))
  27.   ;;
  28.   ;; will return nil
  29.   ;;
  30.   (and
  31.     (member
  32.       T
  33.       (mapcar
  34.         (function
  35.           (lambda (x)
  36.             (= a (eval x)))) l))) )   ;;   /*}}}*/


I hope my analysis of your code wasn't too far off base (very rusty at reading lisp code) and these procedures help a little.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

AIberto

  • Guest
Re: Check data ,Return "T" or "nil " I'm in trouble
« Reply #10 on: July 10, 2015, 12:09:17 PM »
Please don't ask me to do any Lisp code (I havnen't in years and wouldn't be of any use) but if I may make a suggestion:
Read the contents of the file into a list and check the items in the list. The reason being it would be faster if you read contents and operate on a list instead of trying to check as you are reading the file.

I do have these snips of code you can use (left over from my lisping days):


I hope my analysis of your code wasn't too far off base (very rusty at reading lisp code) and these procedures help a little.

Hi John ,  Thanks

AIberto

  • Guest
Re: Check data ,Return "T" or "nil " I'm in trouble
« Reply #11 on: July 15, 2015, 12:49:10 PM »
Code at #3 returns T. Is it correct?


Dear kpblc,

eg: data.txt
Code: [Select]
ABCDEF123
0
3012400
3024000

If I want  only change the value of Second row , Other values unchanged

Code: [Select]
ABCDEF123
0 ==>change to 3018059
3012400
3024000

Have a quick way ? Thanks.

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Check data ,Return "T" or "nil " I'm in trouble
« Reply #12 on: July 15, 2015, 01:07:54 PM »
HINT: Look in CADR & SUBST

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Check data ,Return "T" or "nil " I'm in trouble
« Reply #13 on: July 15, 2015, 04:28:44 PM »
Code at #3 returns T. Is it correct?


Dear kpblc,

eg: data.txt
Code: [Select]
ABCDEF123
0
3012400
3024000

If I want  only change the value of Second row , Other values unchanged

Code: [Select]
ABCDEF123
0 ==>change to 3018059
3012400
3024000

Have a quick way ? Thanks.

You would need to read & re-write the file, e.g.:

Code - Auto/Visual Lisp: [Select]
  1. (defun changeline ( txt lin new / des lst str )
  2.     (if
  3.         (and
  4.             (setq txt (findfile txt))
  5.             (setq des (open txt "r"))
  6.         )
  7.         (progn
  8.             (while (setq str (read-line des))
  9.                 (if (= 0 (setq lin (1- lin)))
  10.                     (setq lst (cons new lst))
  11.                     (setq lst (cons str lst))
  12.                 )
  13.             )
  14.             (close des)
  15.             (if (setq des (open txt "w"))
  16.                 (progn
  17.                     (foreach str (reverse lst)
  18.                         (write-line str des)
  19.                     )
  20.                     (close des)
  21.                     new
  22.                 )
  23.             )
  24.         )
  25.     )
  26. )

AIberto

  • Guest
Re: Check data ,Return "T" or "nil " I'm in trouble
« Reply #14 on: July 15, 2015, 09:46:13 PM »
HINT: Look in CADR & SUBST

Thanks ,ronjonp , SUBST  is useful !