dan_g8 Posted February 17, 2015 Posted February 17, 2015 Hi, i have a txt file that contains data for my drawing, each line of the text file contains a different number of characters ..... i need to amend the text file (using spaces at the end of the data) so that every line is 256 characters long so that when i import my data everything lines up correctly. what would be the best/easiest way of doing this? for example one line may contain 200 characters, i would need to add 56 spaces onto the end of the line. Thanks Dan Quote
pBe Posted February 17, 2015 Posted February 17, 2015 Guaranteed no lines more that 256 on the text file? Quote
hmsilva Posted February 17, 2015 Posted February 17, 2015 (edited) Dan, if I understood correctly, perhaps something like this will do the trick... (defun c:demo (/ a b c f l n) (if (and (setq f (getfiled "Select file" "" "txt" 0)) (setq n (getint "\n Enter new line length: ")) ) (progn (setq a (open f "r")) (while (setq b (read-line a)) (if (< (setq l (strlen b)) n) (progn (repeat (- n l) (setq b (strcat b " ")) ) (setq c (append c (list b))) ) (setq c (append c (list b))) ) ) (close a) (setq a (open f "w")) (foreach b c (write-line b a) ) (close a) ) ) (princ) ) Hope that helps Henrique Edited February 17, 2015 by hmsilva typo Quote
pBe Posted February 17, 2015 Posted February 17, 2015 Dan, (defun c:demo (/ a b c f l n)...... [/quote] Oh man, you guys got too much on your hands I was thinking of create the space pad THEN one [i]strcat[/i] EDIT: like this [code] (defun pad (str) (Strcat str (substr " " (strlen str) ) ) ) EDIT2: Granting there are lines more than the desired number of string We did not get a response from the OP if the lines are more than 256, should the string beyond 256 be a new line? or leave it as it is? (like what Henrique posted). Quote
hmsilva Posted February 17, 2015 Posted February 17, 2015 Oh man, you guys got too much on your hands I was thinking of create the space pad THEN one strcat Hi pBe! Nice to see you back! Granting there are lines more than the desired number of string We did not get a response from the OP if the lines are more than 256, should the string beyond 256 be a new line? or leave it as it is? (like what Henrique posted). Good point! Let's wait and see the OP reply... Cheers Henrique Quote
pBe Posted February 17, 2015 Posted February 17, 2015 Hi pBe!Nice to see you back! Thank you Henrique, Nice to be seen, I'm always around [lurking and observing], I just don't have the downtime to play as i use to Quote
dan_g8 Posted February 17, 2015 Author Posted February 17, 2015 Thank shmsilva, that looks perfect ... however i am getting an "; error: bad argument type: stringp nil" on the second "(setq c (append c (list b)))" what have i done wrong?? thank you! PS it will never go above 256 characters. Quote
hmsilva Posted February 17, 2015 Posted February 17, 2015 Thank shmsilva, that looks perfect ... however i am getting an "; error: bad argument type: stringp nil" on the second "(setq c (append c (list b)))"what have i done wrong?? thank you! PS it will never go above 256 characters. You're welcome, Dan! You you should not get any error on the second 'append', it's only to append the list, if the string have more characters then the value setted in variable 'n'... I can not reproduce that error. Henrique Quote
Lee Mac Posted February 17, 2015 Posted February 17, 2015 When the string manipulation is concerned, this thread may be of interest: http://www.theswamp.org/index.php?topic=47641.5 Quote
hmsilva Posted February 17, 2015 Posted February 17, 2015 When the string manipulation is concerned, this thread may be of interest: Nice stuff! Thank you Lee. Henrique Quote
Lee Mac Posted February 17, 2015 Posted February 17, 2015 Nice stuff!Thank you Lee. Thanks Henrique! Here's another way to write this program (using a similar method to that proposed by pBe above): (defun c:padfile ( / ds1 ds2 fn1 fn2 spc str ) (if (and (setq fn1 (getfiled "Select file" "" "txt" 0)) (setq ds1 (open fn1 "r")) (setq fn2 (vl-filename-mktemp (vl-filename-base fn1) (vl-filename-directory fn1) ".txt")) (setq ds2 (open fn2 "w")) ) (progn (setq spc " ") (repeat 8 (setq spc (strcat spc spc))) (while (setq str (read-line ds1)) (write-line (substr (strcat str spc) 1 256) ds2) ) ) ) (foreach x (list ds1 ds2) (if (= 'file (type x)) (close x)) ) (princ) ) Quote
Lee Mac Posted February 17, 2015 Posted February 17, 2015 Thank shmsilva, that looks perfect ... however i am getting an "; error: bad argument type: stringp nil" on the second "(setq c (append c (list b)))" what have i done wrong?? You you should not get any error on the second 'append', it's only to append the list, if the string have more characters then the value setted in variable 'n'...I can not reproduce that error. I believe the error stems from this expression: (setq a (open myfile "w")) Variable 'myfile' is not defined. Quote
hmsilva Posted February 17, 2015 Posted February 17, 2015 I believe the error stems from this expression: (setq a (open myfile "w")) Variable 'myfile' is not defined. You're correct Lee, my bad... I could not reproduce the error, because I was testing with my original code, with the original variables... Code in post #3, corrected. Thank you Lee! Henrique Quote
pBe Posted February 18, 2015 Posted February 18, 2015 ... (setq fn2 (vl-filename-mktemp (vl-filename-base fn1) (vl-filename-directory fn1) ".txt")) (setq ds2 (open fn2 "w"))... Very nice Quote
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.