daved
New Member
Posts: 24
|
Post by daved on Feb 13, 2024 9:20:21 GMT -5
I'm writing a list of names and numbers to a file. I need the number to line up in columns like this:
1 Abcd, Jason 1401 1257
2 Afhff, Gary 1373 1031
3 Ahddk, Aaron 1778 1728
4 Bshs, Luigi 1001 1096
5 Bsshhsh, Christopher 1760 1710
All I can get is this:
1 Abcd, Jason 1401 1257
2 Afhff, Gary 1373 1031
3 Ahddk, Aaron 1778 1728
4 Bshs, Luigi 1001 1096
5 Bsshhsh, Christopher 1760 1710
How can I get aligned columns in the file?
|
|
|
Post by tsh73 on Feb 13, 2024 9:44:27 GMT -5
You can use some user-difined functions to pad output with spaces This is result of what I have saved, code below
>Source <>Pad right < > Pad left< ------------------------------------------------------------- >Jack <>Jack < > Jack< >and <>and < > and< >Jill <>Jill < > Jill< >went <>went < > went< >up <>up < > up< >the <>the < > the< >hill <>hill < > hill< >To fetch a pail of water. <>To fetch a< >To fetch a<
'demo data Jack, "and", Jill, went, up, the, hill, "To fetch a pail of water.", "**" 'Short strings to be paddeed with spaces, long string to be cut by design.
open "test.txt" for output as #1
print #1, ">Source <>Pad right < > Pad left<" print #1, "-------------------------------------------------------------"
while 1 read a$ if a$="**" then exit while print #1, ">";padr$(a$, 30);"<";">";padr$(a$,10);"< >";padl$(a$,10);"<"
wend
close #1 run "notepad.exe ";DefaultDir$;"\";"test.txt"
end
'functions '--------------------------------------------- 'adds spaces from the left until 'n' symbols 'if n<len(a$) returns left$(a$,n) function padl$(a$,n) padl$ = left$(space$(n-len(a$))+a$,n) end function
'adds spaces from the right until 'n' symbols 'if n<len(a$) returns left$(a$,n) function padr$(a$,n) padr$ = left$(a$+space$(n-len(a$)),n) end function
|
|
daved
New Member
Posts: 24
|
Post by daved on Feb 13, 2024 10:03:05 GMT -5
Thanks! I'll try that.
|
|
daved
New Member
Posts: 24
|
Post by daved on Feb 13, 2024 14:26:28 GMT -5
Works Great tsh73 -- Thanks!
|
|
|
Post by badbug on Feb 15, 2024 16:58:45 GMT -5
Something I wrote years ago and it still works. You can align "r"ight, "l"eft, or "c"enter for a given length
' ------------------------------------- ' Align fld$ to left right or center ' of a given width ' ------------------------------------- FUNCTION align$(fld$,width,lrc$) s = width - len(fld$) fld$ = left$(fld$,width) if s < 1 then align$ = fld$ else b$ = space$(width) if lrc$ = "l" then align$ = fld$;left$(b$,s) if lrc$ = "r" then align$ = left$(b$,s);fld$ if lrc$ = "c" then align$ = left$(b$,int(s / 2));fld$;left$(b$,int(s / 2) + (s and 1)) end if END FUNCTION
|
|