Align columns: Difference between revisions
Content added Content deleted
No edit summary |
(Align columns in Yabasic) |
||
Line 10,423: | Line 10,423: | ||
justified, right justified, or center justified within its column. |
justified, right justified, or center justified within its column. |
||
</pre> |
</pre> |
||
=={{header|Yabasic}}== |
|||
{{trans|Run BASIC}} |
|||
<lang yabasic>theString$ = "Given$a$text$file$of$many$lines,$where$fields$within$a$line$" |
|||
theString$ = theString$ + "are$delineated$by$a$single$'dollar'$character,$write$a$program" |
|||
theString$ = theString$ + "that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$" |
|||
theString$ = theString$ + "column$are$separated$by$at$least$one$space." |
|||
theString$ = theString$ + "Further,$allow$for$each$word$in$a$column$to$be$either$left$" |
|||
theString$ = theString$ + "justified,$right$justified,$or$center$justified$within$its$column." |
|||
x = shoTable(theString$, "left", 6) |
|||
x = shoTable(theString$, "right", 6) |
|||
x = shoTable(theString$, "center", 6) |
|||
end |
|||
sub word$(sr$, wn, delim$) |
|||
local i, j, n, sd, sl, sl2 |
|||
local s$, res$, d$ |
|||
d$ = delim$ |
|||
j = wn |
|||
if j = 0 j = j+1 |
|||
res$ = "" : s$ = sr$ |
|||
if d$ = "" d$ = " " |
|||
sd = len(d$) : sl = len(s$) |
|||
do |
|||
n = instr(s$,d$) |
|||
j = j - 1 |
|||
if j = 0 then |
|||
if n = 0 then res$ = s$ else res$ = mid$(s$, 1, n-1) : fi |
|||
return res$ |
|||
fi |
|||
if n = 0 return res$ |
|||
if n = sl-sd then res$ = "" : return res$ : fi |
|||
sl2 = sl-n |
|||
s$ = mid$(s$, n+1, sl2) |
|||
sl = sl2 |
|||
loop |
|||
return res$ |
|||
end sub |
|||
sub shoTable(theString$, align$, across) |
|||
local i, a$, b$ |
|||
print "------------ align:", align$, " -- across:", across, " ------------" |
|||
dim siz(across) |
|||
b$ = " " |
|||
while word$(theString$, i+1, "$") <> "" |
|||
siz(mod(i, across)) = max(siz(mod(i, across)), len(word$(theString$, i+1, "$"))) |
|||
i = i+1 |
|||
wend |
|||
for i = 0 to across - 1 |
|||
siz(i) = siz(i)+1 |
|||
if siz(i) and 1 siz(i) = siz(i)+1 |
|||
next i |
|||
i = 0 |
|||
a$ = word$(theString$, i+1, "$") |
|||
while a$ <> "" |
|||
s = siz(mod(i, across)) - len(a$) |
|||
if align$ = "right" a$ = left$(b$, s) + a$ |
|||
if align$ = "left" a$ = a$ + left$(b$, s) |
|||
if align$ = "center" a$ = left$(b$, int(s / 2)) + a$ + left$(b$, int(s / 2) + (s and 1)) |
|||
print "|", a$; |
|||
i = i + 1 |
|||
if mod(i, across) = 0 print "|" |
|||
a$ = word$(theString$, i+1, "$") |
|||
wend |
|||
print |
|||
end sub</lang> |
|||
{{out}} |
|||
<pre>Same as Run BASIC entry.</pre> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |