Author Topic: Sort txt file  (Read 3241 times)

0 Members and 1 Guest are viewing this topic.

Tommy

  • Guest
Sort txt file
« on: November 30, 2004, 09:27:33 PM »
I have extracted attribute text to a .txt file from a number of blocks in a dwg
now the block is the same but the attribute text can be different
its a Bolt bom
this is a sample of the .txt file
Qty   Dia   Proc   Length   Finish   Bolt   Nut   Washers
2   M20   8.8/s   50   GALV   B/   N/   W
4   M16   4.6/s   50   GALV   B/   N/   W
4   M20   8.8/s   50   GALV   B/   N/   W
4   M16   4.6/s   50   GALV   B/   N/   W
4   M20   8.8/s   50   GALV   B/   N/   W

Now I would like to make a summary of this in lsp have/has anyone sorted a list like this and has a sample to work off  :?:

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Sort txt file
« Reply #1 on: November 30, 2004, 09:57:48 PM »
I assume the lisp will read the text file and get strings like this:
"2 M20 8.8/s 50 GALV B/ N/ W "
"4 M16 4.6/s 50 GALV B/ N/ W "

You can convert these into list like this
("2" "M20" "8.8/s" "50" "GALV" "B/" "N/" "W")
("4" "M16" "4.6/s" "50" "GALV" "B/" "N/" "W")

It is then easy to sort on the second item.

Or if the size allways starts with an M you can grab the M and the next
2 or 3 characters & sort on that.

You are sorting acording to size right? How about length?
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.

Tommy

  • Guest
Sort txt file
« Reply #2 on: November 30, 2004, 10:08:05 PM »
The list is allready in this format
"You can convert these into list like this
("2" "M20" "8.8/s" "50" "GALV" "B/" "N/" "W")
("4" "M16" "4.6/s" "50" "GALV" "B/" "N/" "W") "
to sort them in a summary M20x50 8.8/s Galv BNW

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Sort txt file
« Reply #3 on: November 30, 2004, 10:42:34 PM »
Something like this?
Code: [Select]
(defun c:test ()
  (setq strlst (list '("2" "M20" "8.8/s" "50" "GALV" "B/" "N/" "W")
                     '("4" "M16" "4.6/s" "50" "GALV" "B/" "N/" "W")
                     '("4" "M20" "8.8/s" "50" "GALV" "B/" "N/" "W")
                     '("4" "M16" "4.6/s" "50" "GALV" "B/" "N/" "W")
                     '("4" "M20" "8.8/s" "50" "GALV" "B/" "N/" "W"))
  )

  (setq newlst '())
  (foreach x strlst
    (setq x1 (list (car x)
                   (strcat (nth 1 x) "x" (nth 3 x))
                   (nth 2 x)
                   (strcat (substr (nth 5 x) 1 1)
                           (substr (nth 6 x) 1 1)
                           (nth 7 x)
                   )
             )
    )
    (setq newlst (cons x1 newlst))
  )
  (setq newlst (vl-sort newlst '(lambda (e1 e2) (< (cadr e1) (cadr e2)))))
  (foreach x newlst
    (print x)
  )
  (princ)
)

Code: [Select]
Command: test

("4" "M16x50" "4.6/s" "BNW")
("4" "M16x50" "4.6/s" "BNW")
("4" "M20x50" "8.8/s" "BNW")
("4" "M20x50" "8.8/s" "BNW")
("2" "M20x50" "8.8/s" "BNW")
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.

Tom

  • Guest
Sort txt file
« Reply #4 on: November 30, 2004, 10:58:25 PM »
Not quite they need to be added together to summarise
ie
("4" "M16x50" "4.6/s" "BNW")
("4" "M16x50" "4.6/s" "BNW")
("4" "M20x50" "8.8/s" "BNW")
("4" "M20x50" "8.8/s" "BNW")
("2" "M20x50" "8.8/s" "BNW") would become

("8" "M16x50" "4.6/s" "BNW")
("10" "M20x50" "8.8/s" "BNW")

Ron Heigh

  • Guest
Sort txt file
« Reply #5 on: November 30, 2004, 11:51:23 PM »
you can do this in excel
open a blank excel file
select open
choose tab delineated
createa a pivot table to summarize the items

Tom

  • Guest
Sort txt file
« Reply #6 on: December 01, 2004, 12:37:28 AM »
At this point in time I Don't want to use Excel

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Sort txt file
« Reply #7 on: December 01, 2004, 08:42:45 AM »
Not the prettiest code in the world, there are some here that could do
a more eloquent job.
Code: [Select]
(defun c:test ()
  (setq strlst (list '("2" "M20" "8.8/s" "50" "GALV" "B/" "N/" "W")
                     '("4" "M16" "4.6/s" "50" "GALV" "B/" "N/" "W")
                     '("4" "M20" "8.8/s" "50" "GALV" "B/" "N/" "W")
                     '("4" "M16" "4.6/s" "50" "GALV" "B/" "N/" "W")
                     '("4" "M20" "8.8/s" "50" "GALV" "B/" "N/" "W"))
  )

  (setq newlst '())
  (foreach x strlst
    (setq x1 (list (car x)
                   (strcat (nth 1 x) "x" (nth 3 x))
                   (nth 2 x)
                   (strcat (substr (nth 5 x) 1 1)
                           (substr (nth 6 x) 1 1)
                           (nth 7 x)
                   )
             )
    )
    (setq newlst (cons x1 newlst))
  )
  (setq newlst (vl-sort newlst '(lambda (e1 e2) (< (cadr e1) (cadr e2)))))

  (setq shortlst '()
        idx (length newlst)
        tmp nil
  )
  (while (>= (setq idx (1- idx)) 0)
    (setq x (nth idx newlst))
    (if (and tmp
             (= (cadr tmp) (cadr x))
        )
      (setq tmp (cons(itoa (+ (atoi (car tmp)) (atoi (car x)))) (cdr tmp)))
      (setq shortlst (cons tmp shortlst)
            tmp x)
    )
    (if (= idx 0)
      (setq shortlst (vl-remove nil (cons tmp shortlst)))
    )
  )

  (foreach x shortlst
    (print x)
  )
  (princ)
)
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.

Tom

  • Guest
Sort txt file
« Reply #8 on: December 02, 2004, 12:51:09 AM »
"Not the prettiest code in the world"
But it does work and thats what counts
Thanks CAB