svorgodne Posted October 7, 2010 Share Posted October 7, 2010 How can I import a TXT file into a variable with autolisp? so far I have been able to do something like this: (setq x (open "C:/Documents and Settings/sgonzalez/My Documents/new_02.txt")) but the result is: Command: !x # instead of the real content of the txt file. Can anybody help me please? Like always, it is urgent Thanks Quote Link to comment Share on other sites More sharing options...
Se7en Posted October 7, 2010 Share Posted October 7, 2010 (defun iniread (str file) ;; iniread ;; ;; Returns all valid entries in a file. (see below) ;; ;; file to be read must be structured in a way that ;; all lines begining with a `` ; '' is considered ;; to be a comment. ;; ;; EX: ;; Example file: Example.ini ;; ;; ; this is an example ini file. ;; var1=val1 ;; var2=val2 ;; ; var3=val3 ;; var4=val4 ;; ;; ( (lambda ( / f my-list ) ;; (setq f (open "c:\\Example.ini" "R")) ;; (setq my-list ;; (apply ;; 'append ;; (iniread (read-line f) f))) ;; (close f) ;; my-list ) ) ;; ;; => ("var1=val1" "var2=val2" "var4=val4") ;; ;; By: Se7en ;; ( (lambda (s f) (if s (cons (if (not (wcmatch s "*;*")) (list s)) (iniread (read-line f) f)))) str file ) ) Quote Link to comment Share on other sites More sharing options...
BlackBox Posted October 7, 2010 Share Posted October 7, 2010 (edited) First, please use code tags in your posts. When opening a file, remember to add the mode (read, write, append), in this situation you would use this: (setq x (open "C:/Documents and Settings/sgonzalez/My Documents/new_02.txt" "[color=red]R[/color][color=black]"[/color])) Try using the read-line function to get the content you want, then remember to always use the close function for your text file. Hope this helps! Edited October 7, 2010 by BlackBox Forgot about the "quotes" Quote Link to comment Share on other sites More sharing options...
svorgodne Posted October 7, 2010 Author Share Posted October 7, 2010 Thanks both of you.. So far anyhow, I just forgot to add the mode in the previous message, but I didn't in the programming, so at the end the result is still: Command: !x # Obviously the txt file has a list I want to compare with some other information inside the drawing. On the other hand Se7en, as far as I understand, the only valid programming in your message is: (defun iniread (str file) ( (lambda (s f) (if s (cons (if (not (wcmatch s "*;*")) (list s)) (iniread (read-line f) f)))) str file ) ) Am I right? the rest is only commented and when I run the "iniread" function this is the result: Command: (iniread) ; error: too few arguments Any other ideas? Thanks in advance again Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted October 7, 2010 Share Posted October 7, 2010 (edited) Why recursion Se7en? Surely that is going to dump a lot of information on the stack and will bum out pretty quick if the file is of adequate size? (defun _read ( filename / openfile line lst ) (cond ( (setq openfile (open filename "r")) (while (setq line (read-line openfile)) (setq lst (cons line lst)) ) (close openfile) (reverse lst) ) ) ) Edited October 7, 2010 by Lee Mac Quote Link to comment Share on other sites More sharing options...
svorgodne Posted October 7, 2010 Author Share Posted October 7, 2010 Thank you all guys, I just figured it out and it was way too simple here it is the code I used (setq x (open "C:/Documents and Settings/sgonzalez/My Documents/new_02.txt" "r")) (setq y (read-line x)) (close x) Cheers Quote Link to comment Share on other sites More sharing options...
BlackBox Posted October 7, 2010 Share Posted October 7, 2010 Thank you all guys, I just figured it out and it was way too simple Hooray! And I reiterate... please use code tags in your posts. Quote Link to comment Share on other sites More sharing options...
Se7en Posted October 7, 2010 Share Posted October 7, 2010 Why recursion Se7en? Surely that is dump a lot of information on the stack and will bum out pretty quick if the file is of adequate size? um, sure, i suppose it could but it would take a pretty big file i suppose. Besides, recursion isnt all evil; *lol* that procedure may be using recursion but it is still really fast. For example, I took yours and mine and pinned them up against each other on a file with 10,000 lines in it and I'm right on your tail. ;;;;;;;;; --- P R O F I L E L I S P --- ;;;;;;;;;;; ;; by (C.) Vladimir Nesterovsky, December 1998. ;; Free for *non-commercial* personal use only ;; with this notice unchanged ------------------------------------------------------ TIMER_FOR_LM:READ: n=1, t=0.0100181, t/call=0.0100181. TIMER_FOR_J7:READ: n=1, t=0.0119895, t/call=0.0119895. Fastest abstraction: "LM:READ" With a time of: 0.0100181 ------------------------------------------------------ ... ------------------------------------------------------ TIMER_FOR_LM:READ: n=1, t=0.00901222, t/call=0.00901222. TIMER_FOR_J7:READ: n=1, t=0.0109836, t/call=0.0109836. Fastest abstraction: "LM:READ" With a time of: 0.0090122 ------------------------------------------------------ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.