Text output to a file with tabs

Odie Silva

Usenet Archive
Joined
Jul 21, 2003
Messages
25
Reaction score
0
Hi,
I am writing text lines to a file but I am having trouble aligning the colums using TAB or space.
Exemple:

LAYER = 140 COLOR = 7 LINETYPE = "CONTINUOUS" LAYER = 800 COLOR = 7 LINETYPE = "CONTINUOUS"

but if a layer name is longer it will start the tab and get the columns out of alingment

LAYER = BOUNDARY COLOR = 7 LINETYPE = "WATER"

How to tell the program to start at a particular space number?

Odie
 
Cool Jesus,

But I never connected my database with Excell but I know if can be accomplished with startapp.
Do you have any sample?

tIA

Odie
 
No direct way to assign (column#). You will have to 'pad' each item with spaces (chr 32) for a finite length, append next item (padded), etc. Then output the (strcat item1 item2). You will also need a Font in your text Editor that is pretty much a 'block' type font - with width about the same. Problem is that a "i" is much thinner than a "w". Gets jagged looking. Another approach? Write to a file.csv (comma seperated) like:
(setq Output (strcat (chr 34) item1 (chr 34) "," (chr 34) item2 (chr 34) ",")) (write-line outputfile Output). Then, when done, double click to take it into Excel.
Bob
 
See if this helps you. The "\t" is for tab sapce.

(setq temp (open "TEST.txt" "w"))
(princ (strcat "\nCol1" "\t" "Col2") temp)
(princ (strcat "\nCol1" "\t" "Col2" "\t" "Col3") temp)
(close temp)
 
It seems that would be easier to control the colunm aligment within Lisp. I thought about the "padding" of each between two colunms by the following expression:

(strcat lay_name
(repeat spacing (chr 32))
(itoa (cdr (assoc 62 lay)))
(repeat spacing (chr 32))
(cdr (assoc 6 lay))
)

but it is flawed...why?

Odie
 
Ken,

The approach you suggested works fine just like the one I started with, but if the layer name passes the tab mark it will add the requested number of tabs producing the following:

LAYER = 830 COLOR = 64 LINETYPE = DEP
LAYER = 840 COLOR = 7 LINETYPE = CONTINUOUS LAYER = 850 COLOR = 42 LINETYPE = CONTINUOUS LAYER = 860 COLOR = -46 LINETYPE = CONTINUOUS LAYER = 870 COLOR = 7 LINETYPE = CONTINUOUS LAYER = 965 COLOR = 2 LINETYPE = CONTINUOUS LAYER = 970 COLOR = 33 LINETYPE = CONTINUOUS LAYER = 975 COLOR = 33 LINETYPE = CONTINUOUS LAYER = Boundary COLOR = 7 LINETYPE = CONTINUOUS LAYER = BREAKLINES COLOR = 7 LINETYPE = CONTINUOUS LAYER = BRK2 COLOR = 7 LINETYPE = CONTINUOUS LAYER = BRK3 COLOR = 7 LINETYPE = CONTINUOUS

Odie
 
Using spaces is not a good way to create columns. As Bob said, charactors have different widths. To make sure you have enough tabs, you will have to evaluate the lengths of your strings before strcating. If the longest string is, say, over 5 charactors then put two tab spaces in.
 
I agree with Ken.
To check the length:
(setq LS (strlen col1))
(if (< LS 5)
(setq col1 (strcat col1 "\t"))
(setq col1 (strcat col1 "\t" "\t"))
)
continued..
Bob
 
I finally "got" what your delma was. I put this together, probably a little rough, but it works in notepad where the tab spacing is 8 charactors. You would think there is an easier way, but my brain got stuck heading in this direction. Maybe it will give you some ideas.

(defun C:write_columns
(setq col1list '("975" "Boundary" "Breaklines")
col2list '("33" "33" "7")
col3list '("continuous" "continuous" "DEP")
col1len (apply 'max (mapcar 'strlen col1list))
col2len (apply 'max (mapcar 'strlen col2list)))
(setq temp (open "TEST.txt" "w"))
(repeat (length col1list)
(setq col1val (car col1list)
col2val (car col2list)
col3val (car col3list))
(setq col1vallen (strlen col1val)
col2vallen (strlen col2val)
col3vallen (strlen col3val))
(setq tabx1 (+ 8 (* 8 (fix (/ col1len 8))))
tabx2 (+ 8 (* 8 (fix (/ col2len 8)))))
(if (member (- tabx1 (strlen col1val)) '(8 16 24 32 40 48 56
64))
(setq tabs1 -1)
(setq tabs1 0)
)
(if (member (- tabx2 (strlen col2val)) '(8 16 24 32 40 48 56
64))
(setq tabs2 -1)
(setq tabs2 0)
)
(princ "\n" temp)
(princ col1val temp)
(repeat (+ tabs1 (1+ (fix (/ (- tabx1 (strlen col1val)) 8)))) (princ "\t" temp)) (princ col2val temp) (repeat (+ tabs2 (1+ (fix (/ (- tabx2 (strlen col2val)) 8)))) (princ "\t" temp)) (princ col3val temp) (setq col1list (cdr col1list) col2list (cdr col2list) col3list (cdr col3list)) ) (close temp) (princ)
)
 
Thanks Ken,

I think you nailed it!!!
I ran it and it seemed to be the idea I was after :)

It was a good push ahead.

Have a great evening!

Odie
 

Latest threads

Back
Top