Jump to content

Help Breaking Down a List of Strings to Act on the Data Within Each String


Recommended Posts

Posted (edited)

Good morning/afternoon/evening everyone.

I've been digging around the forum to see if I can find a solution to what I need and I've gotten rather close, I'm just not sure how to bring everything together to actually do what I want.

Essentially, I want to go into a .csv, rip the first 2 columns from each row (1st column is a quantity,  2nd column is a filename that corresponds to a .dwg file) and then take the two column values and insert blocks that correspond to the filename in the quantity listed.

Using this code (courtesy of an old thread Tharwat helped out in) I can break the csv down into a list where each member is a string that contains the relevant data from the first two columns separated by a comma. So like ("5,block1" "2,block2" "15,block3"...)

(defun c:ColumnRipTest () 					;;Function courtesy of forum post by Tharwat @ Cadtutor.net
 (if (and (setq o (getfiled "" (getvar 'dwgprefix) "csv" 16))   
          (setq f (open o "r"))					
     )
   (progn
     (while (setq str (read-line f))				
       (if (setq p (vl-string-search "," str 2))		
         (setq lst (cons (substr str 1 p) lst))
       )
     )
     (close f)
   )
 )
 (if lst
   (setq lst (reverse lst))
)
)



Where I'm stuck is taking those individual strings and breaking them apart so I can use the two column values. I expect that once I have a variable holding the count and a variable holding the string that corresponds to the filename I can utilize Lee Mac's Find File script with an insert command thrown in to add what I need.

I've attached a little test csv that, when running the above code, outputs ("a,b" "h,i" "o,p" "v,w") so I'd like to break this further down to act on the comma separated values.

Thanks for any help, and apologies if I'm not too clear, just starting learning LISP earlier this week lol

randomcsvtest.csv

Edited by Kreaten_vhar
Typo
Posted (edited)

Huzzah I figured it out myself, posting the "final" (still gotta test and probably debug some more really) script as an attachment for reference if anyone takes a gander at this thread. Also I'm like 99% sure this could be improved, but I got a literal week of LISP programming under my belt right now so I'm happy it just works!

AllinOne.lsp

Edited by Kreaten_vhar
added words for clarity
Posted

Hi,

You described your csv file into your first post here differently than the attached csv file ! 

Anyway, here is my go with it which is untested so please test it and let me know.

I am sure that you can learn from the following codes since you were able to come up with working method. :) 

(defun c:Test ( / *error* csv opn ins str pos qty bkn )
  ;;------------------------------------------------------------;;
  ;;	Author: Tharwat Al Choufi - Date: 19.Apr.2022		;;
  ;;	website: https://autolispprograms.wordpress.com		;;
  ;;------------------------------------------------------------;;
  (defun *error* (msg)
    (and opn (= (type opn) 'FILE) (close opn))
    (princ "\n*Cancelled*")
    )
 (and (setq csv (getfiled "" (getvar 'dwgprefix) "csv" 16))
      (setq opn (open csv "r"))
      (setq ins '(5.0 0.0 0.0))
      (or (while (setq str (read-line opn))
            (and (setq pos (vl-string-search "," str 9))
                 (setq qty (atoi (substr str 1 (vl-string-search "," str))))
                 (tblsearch "BLOCK" (setq bkn (substr str 1 pos)))
                 (repeat qty
                   (command "_.-INSERT" bkn "_non" ins "1" "1" "0")
                   (setq ins (polar ins 0.0 5.0))
                   )
                 )
            )
          t
          )
      (setq opn (close opn))
      )
  (princ)
  ) (vl-load-com)

 

  • Like 1
Posted

Hey Tharwat, thank you so much for providing a function I can look over and learn from.

And yeah,  what I described and the CSV I posted where indeed different, the one I was posted was arbitrary for as long as the first two columns are acted upon in the way described I'm happy :D

Thanks again everyone who replied, LISP is a surprisingly rewarding language to learn and I'm looking forward to getting better!

Posted

Just another suggestion say you have csv "1,2,3,4,5,6,7" convert to a list as suggested (1 2 3 4 5 6 7) so I don't want 1st 2 items I use the nth function on the list the only thing to remember is that the 1st item is zero not 1.

 

(nth 2 lst) = 3

(nth 4 lst)= 5

 

and so on.

 

You can also look at car functions

 

(car lst) = 1

(cadr lst) = 2

(caddr lst) = 3

 

and so on.

 

  • Like 1
Posted
On 4/20/2022 at 12:14 PM, BIGAL said:

Just another suggestion say you have csv "1,2,3,4,5,6,7" convert to a list as suggested (1 2 3 4 5 6 7) so I don't want 1st 2 items I use the nth function on the list the only thing to remember is that the 1st item is zero not 1.

 

(nth 2 lst) = 3

(nth 4 lst)= 5

 

and so on.

 

You can also look at car functions

 

(car lst) = 1

(cadr lst) = 2

(caddr lst) = 3

 

and so on.

 

(nth 0 lst) = 1

(nth 1 lst )= 2

(nth 2 lst )= 3

How about this?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...