Author Topic: [Help Me]How to deal with these data?  (Read 4205 times)

0 Members and 1 Guest are viewing this topic.

myloveflyer

  • Newt
  • Posts: 152
Re: [Help Me]How to deal with these data?
« Reply #15 on: May 06, 2012, 11:47:22 PM »
CAB, program there is local error, please see screenshot ( ring red circle problem).
1.TT175.5x3.75is behind the 2spaces, the output of the program is1 spaces.
2.TT2,114x70 /20 and above alignment after becoming aligned.
3.TT3, the red circle should be equal to the digital one digital sum.
4.The number of spaces between columns and some original data format is not consistent.
Really thank you for help me to edit this procedure, thank you!
« Last Edit: May 06, 2012, 11:54:36 PM by myloveflyer »
Never give up !

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: [Help Me]How to deal with these data?
« Reply #16 on: May 07, 2012, 11:15:07 AM »
I made one correction to the routine & added some comments at the new file output code.
This table should explain how my code works.
My output does match the formatting of the OLD file.   
Code: [Select]
(defun c:cnvdata (/ alllines fn oldfname newfname ln x newlines
                  ADDLIST ADD_WT DATA_LIST F I LST MINUS_LIST MINUS_N
                  MINUS_WT MIN_WT N NEW_WT OLD_WT WT WT_LIST)
  (setq oldfname "d:/old.DAT")
  (setq newfname "d:/new.DAT")

  (defun sparsers (str delim / ptr lst) ; CAB
    (while (setq ptr (vl-string-search delim str))
      (setq lst (cons (substr str 1 ptr) lst))
      (setq str (substr str (+ ptr 2)))
    )
    (reverse (cons str lst))
  )

 
  ;;  CAB 02/09/06 - Pad Left side of string with spaces
  (defun padR (word len / spaces)
    (repeat (- len (strlen word)) (setq spaces (cons 32 spaces)))
    (strcat word (vl-list->string spaces))
  )

  ;;  CAB 02/09/06 - Pad Left side of string with spaces
  (defun padL (word len / spaces)
    (repeat (- len (strlen word)) (setq spaces (cons 32 spaces)))
    (strcat (vl-list->string spaces) word)
  )
 
  (defun MakeNum (lst) ; convert items 5, 9, 10 to numbers
    (list
      (car lst)
      (cadr lst)
      (caddr lst)
      (cadddr lst)
      (nth 4 lst)
      (atoi (nth 5 lst))
      (nth 6 lst)
      (nth 7 lst)
      (nth 8 lst)
      (atof (nth 9 lst))
      (atof (nth 10 lst))
    )
  )

 
  (if (and (setq fn (findfile oldfname))
           (setq fn (open fn "r"))
      )
    (progn
      (princ "\nData file open success!\n")
      (while (setq ln (read-line fn)) ;  read all the lines in the file
        (setq alllines (cons ln alllines))
      )
      (close fn) ; close the open file handle

      (foreach ln alllines
        (setq lst (vl-remove "" (sparsers ln " ")))
        (cond
          ((= (length lst) 10) ; add 3rd item
           (setq data_list (cons (makeNum (append (list (car lst)(cadr lst) "  ") (cddr lst))) data_list))
          )
          ((= (length lst) 3)(setq data_list (cons lst data_list)))
          ((setq data_list (cons (makeNum lst) data_list)))
        )
      )



 (setq n (length data_list))
  (setq wt_list (nth (- n 1) data_list))
  (setq old_wt (atof (nth 2 wt_list)))   ; get 3rd item
  (setq new_wt (* old_wt 1.05))
  (setq wt old_wt)
  (setq min_wt (* old_wt 0.001))
  (setq i 1)

 
  (while (> (abs (- wt new_wt)) min_wt)
    (setq minus_list (nth i data_list))
    (setq minus_n (nth 5 minus_list))
    (if (> minus_n 1)
      (progn
        (setq addlist (nth (+ i 1) data_list))
        (setq minus_wt (nth 9 minus_list))
        (setq add_wt (nth 9 addlist))
        (setq wt (cal "wt - minus_wt + add_wt"))
        (setq minus_list
               (list
                 (nth 0 minus_list)
                 (nth 1 minus_list)
                 (nth 2 minus_list)
                 (nth 3 minus_list)
                 (nth 4 minus_list)
                 (- minus_n 1)
                 (nth 6 minus_list)
                 (nth 7 minus_list)
                 (nth 8 minus_list)
                 minus_wt
                 (- (nth 10 minus_list) minus_wt)
               )
        )
        (setq data_list (subst minus_list (nth i data_list) data_list))
        (setq addlist
               (list
                 (nth 0 addlist)
                 (nth 1 addlist)
                 (nth 2 addlist)
                 (nth 3 addlist)
                 (nth 4 addlist)
                 (+ (nth 5 addlist) 1)
                 (nth 6 addlist)
                 (nth 7 addlist)
                 (nth 8 addlist)
                 add_wt
                 (+ (nth 10 addlist) add_wt)
               )
        )
        (setq data_list (subst addlist (nth (+ i 1) data_list) data_list))
      )
    )
    (setq i (+ i 1))
    (if (= i (- n 2))
      (setq i 1)
    )
  )
  (setq data_list (subst (subst wt old_wt wt_list) wt_list data_list))
     
  (princ (if (setq fn (open "d:/new.DAT" "w"))
           "Data file open success!\n"
           "Data files can't open!\n"
         )
  )
  (foreach record data_list
    (cond
      ((= (length record) 11)
    (princ (padL (car record) 4) fn) ; col 1, RIGHT justify 4 charcters wide
    (princ "    " fn) ; 4 spaces
    (princ (padR (cadr record) 7) fn) ; col 2, LEFT justify 7 charcters wide
    (princ (padR (caddr record) 11) fn) ; col 3, LEFT justify 11 charcters wide
    (princ (cadddr record) fn) ; col 4, no change 4 charcters wide
    (princ "  " fn) ; 2 spaces
    (princ (nth 4 record) fn) ; col 5, no change 4 charcters wide
    (princ (padL (itoa(nth 5 record)) 6) fn) ; col 6, RIGHT justify 6 charcters wide
    (princ "  " fn) ; 2 spaces
    (princ (nth 6 record) fn) ; col 7, no change 3 charcters wide
    (princ "  " fn) ; 2 spaces
    (princ (nth 7 record) fn) ; col 8, no change 5 charcters wide
    (If (vl-string-search "/" (nth 8 record))
      (princ (padL (nth 8 record) 12) fn) ; col 9, RIGHT justify 12 charcters wide
      (princ (padL (strcat(nth 8 record)"   ") 12) fn) ; col 9, RIGHT justify 12 charcters wide
    )
    (princ (padL (rtos(nth 9 record)2 2) 8) fn) ; col 10, RIGHT justify 6 charcters wide
    (princ (padL (rtos(nth 10 record)2 1) 9) fn) ; col 11, RIGHT justify 9 charcters wide
    )
    (; print the last line in the file
     (princ (padL (car record) 2) fn)    ; RIGHT justify 2 characters wide
     (princ (padL (cadr record) 40) fn)  ; RIGHT justify 40 characters wide
     (princ (padL (caddr record) 41) fn) ; RIGHT justify 40 characters wide
     )
      )
    (princ "\n" fn)
  )
  (close fn)

    ) ; progn
    (princ "\nData files can't open!\n")
  ) ; Endif
) ; end defun
« Last Edit: May 07, 2012, 11:34:34 AM by CAB »
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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: [Help Me]How to deal with these data?
« Reply #17 on: May 07, 2012, 11:26:55 AM »
OK I see that Column 9 is justified on the slash character.
I'll have to add some code to get that to happen.

EDIT:
OK updated the code to align column 9 correctly.

BTW in your pictures there looks like a black square in the text in column 3.
This is likely a character that should not be there & could throw off the formatting.
« Last Edit: May 07, 2012, 11:37:16 AM by CAB »
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.

myloveflyer

  • Newt
  • Posts: 152
Re: [Help Me]How to deal with these data?
« Reply #18 on: May 07, 2012, 10:43:21 PM »
CAB, your analysis is right, a black box is a character in the data file, but did not show up, your picture expression is very clear, I can have reference to some other modifications, thank you.
Now there is a problem is, the last line of the last digit if errors (this number should be equal to the output file of the numbers in the eleventh column sum ),
Never give up !

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: [Help Me]How to deal with these data?
« Reply #19 on: May 08, 2012, 08:33:30 AM »
Revised to update the last record correctly.
Code: [Select]
(defun c:cnvdata (/ alllines fn oldfname newfname ln x newlines
                  ADDLIST ADD_WT DATA_LIST F I LST MINUS_LIST MINUS_N
                  MINUS_WT MIN_WT N NEW_WT OLD_WT WT WT_LIST)
  (setq oldfname "d:/old.DAT")
  (setq newfname "d:/new.DAT")

  (defun sparsers (str delim / ptr lst) ; CAB
    (while (setq ptr (vl-string-search delim str))
      (setq lst (cons (substr str 1 ptr) lst))
      (setq str (substr str (+ ptr 2)))
    )
    (reverse (cons str lst))
  )

 
  ;;  CAB 02/09/06 - Pad Left side of string with spaces
  (defun padR (word len / spaces)
    (repeat (- len (strlen word)) (setq spaces (cons 32 spaces)))
    (strcat word (vl-list->string spaces))
  )

  ;;  CAB 02/09/06 - Pad Left side of string with spaces
  (defun padL (word len / spaces)
    (repeat (- len (strlen word)) (setq spaces (cons 32 spaces)))
    (strcat (vl-list->string spaces) word)
  )
 
  (defun MakeNum (lst) ; convert items 5, 9, 10 to numbers
    (list
      (car lst)
      (cadr lst)
      (caddr lst)
      (cadddr lst)
      (nth 4 lst)
      (atoi (nth 5 lst))
      (nth 6 lst)
      (nth 7 lst)
      (nth 8 lst)
      (atof (nth 9 lst))
      (atof (nth 10 lst))
    )
  )

 
  (if (and (setq fn (findfile oldfname))
           (setq fn (open fn "r"))
      )
    (progn
      (princ "\nData file open success!\n")
      (while (setq ln (read-line fn)) ;  read all the lines in the file
        (setq alllines (cons ln alllines))
      )
      (close fn) ; close the open file handle

      (foreach ln alllines
        (setq lst (vl-remove "" (sparsers ln " ")))
        (cond
          ((= (length lst) 10) ; add 3rd item
           (setq data_list (cons (makeNum (append (list (car lst)(cadr lst) "  ") (cddr lst))) data_list))
          )
          ((= (length lst) 3)(setq data_list (cons lst data_list)))
          ((setq data_list (cons (makeNum lst) data_list)))
        )
      )



 (setq n (length data_list))
  (setq wt_list (nth (- n 1) data_list))
  (setq old_wt (atof (nth 2 wt_list)))   ; get 3rd item
  (setq new_wt (* old_wt 1.05))
  (setq wt old_wt)
  (setq min_wt (* old_wt 0.001))
  (setq i 1)

 
  (while (> (abs (- wt new_wt)) min_wt)
    (setq minus_list (nth i data_list))
    (setq minus_n (nth 5 minus_list))
    (if (> minus_n 1)
      (progn
        (setq addlist (nth (+ i 1) data_list))
        (setq minus_wt (nth 9 minus_list))
        (setq add_wt (nth 9 addlist))
        (setq wt (cal "wt - minus_wt + add_wt"))
        (setq minus_list
               (list
                 (nth 0 minus_list)
                 (nth 1 minus_list)
                 (nth 2 minus_list)
                 (nth 3 minus_list)
                 (nth 4 minus_list)
                 (- minus_n 1)
                 (nth 6 minus_list)
                 (nth 7 minus_list)
                 (nth 8 minus_list)
                 minus_wt
                 (- (nth 10 minus_list) minus_wt)
               )
        )
        (setq data_list (subst minus_list (nth i data_list) data_list))
        (setq addlist
               (list
                 (nth 0 addlist)
                 (nth 1 addlist)
                 (nth 2 addlist)
                 (nth 3 addlist)
                 (nth 4 addlist)
                 (+ (nth 5 addlist) 1)
                 (nth 6 addlist)
                 (nth 7 addlist)
                 (nth 8 addlist)
                 add_wt
                 (+ (nth 10 addlist) add_wt)
               )
        )
        (setq data_list (subst addlist (nth (+ i 1) data_list) data_list))
      )
    )
    (setq i (+ i 1))
    (if (= i (- n 2))
      (setq i 1)
    )
  )
  (setq data_list (subst (list (car wt_list)(cadr wt_list) (strcat(rtos wt 2 0)".")) wt_list data_list))
     
  (princ (if (setq fn (open "d:/new.DAT" "w"))
           "Data file open success!\n"
           "Data files can't open!\n"
         )
  )
  (foreach record data_list
    (cond
      ((= (length record) 11)
    (princ (padL (car record) 4) fn) ; col 1, RIGHT justify 4 charcters wide
    (princ "    " fn) ; 4 spaces
    (princ (padR (cadr record) 7) fn) ; col 2, LEFT justify 7 charcters wide
    (princ (padR (caddr record) 11) fn) ; col 3, LEFT justify 11 charcters wide
    (princ (cadddr record) fn) ; col 4, no change 4 charcters wide
    (princ "  " fn) ; 2 spaces
    (princ (nth 4 record) fn) ; col 5, no change 4 charcters wide
    (princ (padL (itoa(nth 5 record)) 6) fn) ; col 6, RIGHT justify 6 charcters wide
    (princ "  " fn) ; 2 spaces
    (princ (nth 6 record) fn) ; col 7, no change 3 charcters wide
    (princ "  " fn) ; 2 spaces
    (princ (nth 7 record) fn) ; col 8, no change 5 charcters wide
    (If (vl-string-search "/" (nth 8 record))
      (princ (padL (nth 8 record) 12) fn) ; col 9, RIGHT justify 12 charcters wide
      (princ (padL (strcat(nth 8 record)"   ") 12) fn) ; col 9, RIGHT justify 12 charcters wide
    )
    (princ (padL (rtos(nth 9 record)2 2) 8) fn) ; col 10, RIGHT justify 6 charcters wide
    (princ (padL (rtos(nth 10 record)2 1) 9) fn) ; col 11, RIGHT justify 9 charcters wide
    )
    (; print the last line in the file
     (princ (padL (car record) 2) fn)    ; RIGHT justify 2 characters wide
     (princ (padL (cadr record) 40) fn)  ; RIGHT justify 40 characters wide
     (princ (padL (caddr record) 40) fn) ; RIGHT justify 40 characters wide
     )
      )
    (princ "\n" fn)
  )
  (close fn)

    ) ; progn
    (princ "\nData files can't open!\n")
  ) ; Endif
) ; end defun
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.

myloveflyer

  • Newt
  • Posts: 152
Re: [Help Me]How to deal with these data?
« Reply #20 on: May 08, 2012, 08:56:58 PM »
CAB, thank you very much for your help, I have been unable to use words to express my gratitude, I'll add some other function procedures, I wish you all the best!


Never give up !

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: [Help Me]How to deal with these data?
« Reply #21 on: May 08, 2012, 10:44:52 PM »
You're welcome, glad to be of help.
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.