Align columns: Difference between revisions
Content added Content deleted
Line 1,023: | Line 1,023: | ||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
==={{header|BBC BASIC}}=== |
|||
{{works with|BBC BASIC for Windows}} |
|||
<lang bbcbasic> DATA 6 |
|||
DATA "Given$a$text$file$of$many$lines,$where$fields$within$a$line$" |
|||
DATA "are$delineated$by$a$single$'dollar'$character,$write$a$program" |
|||
DATA "that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$" |
|||
DATA "column$are$separated$by$at$least$one$space." |
|||
DATA "Further,$allow$for$each$word$in$a$column$to$be$either$left$" |
|||
DATA "justified,$right$justified,$or$center$justified$within$its$column." |
|||
REM First find the maximum length of a 'word': |
|||
max% = 0 |
|||
READ nlines% |
|||
FOR Line% = 1 TO nlines% |
|||
READ text$ |
|||
REPEAT |
|||
word$ = FNword(text$, "$") |
|||
IF LEN(word$) > max% THEN max% = LEN(word$) |
|||
UNTIL word$ = "" |
|||
NEXT Line% |
|||
@% = max% : REM set column width |
|||
REM Now display the aligned text: |
|||
RESTORE |
|||
READ nlines% |
|||
FOR Line% = 1 TO nlines% |
|||
READ text$ |
|||
REPEAT |
|||
word$ = FNword(text$, "$") |
|||
PRINT FNjustify(word$, max%, "left"),; |
|||
UNTIL word$ = "" |
|||
PRINT |
|||
NEXT Line% |
|||
END |
|||
DEF FNword(text$, delim$) |
|||
PRIVATE delim% |
|||
LOCAL previous% |
|||
IF delim% = 0 THEN |
|||
previous% = 1 |
|||
ELSE |
|||
previous% = delim% + LEN(delim$) |
|||
ENDIF |
|||
delim% = INSTR(text$+delim$, delim$, previous%) |
|||
IF delim% = 0 THEN |
|||
= "" |
|||
ELSE |
|||
= MID$(text$, previous%, delim%-previous%) + " " |
|||
ENDIF |
|||
DEF FNjustify(word$, field%, mode$) |
|||
IF word$ = "" THEN = "" |
|||
CASE mode$ OF |
|||
WHEN "center": = STRING$((field%-LEN(word$)) DIV 2, " ") + word$ |
|||
WHEN "right": = STRING$(field%-LEN(word$), " ") + word$ |
|||
ENDCASE |
|||
= word$</lang> |
|||
==={{header|Commodore BASIC}}=== |
==={{header|Commodore BASIC}}=== |
||
Line 1,091: | Line 1,150: | ||
650 data "Further,$allow$for$each$word$in$a$column$to$be$either$left$" |
650 data "Further,$allow$for$each$word$in$a$column$to$be$either$left$" |
||
660 data "justified,$right$justified,$or$center$justified$within$its$column"</lang> |
660 data "justified,$right$justified,$or$center$justified$within$its$column"</lang> |
||
==={{header|BBC BASIC}}=== |
|||
{{works with|BBC BASIC for Windows}} |
|||
<lang bbcbasic> DATA 6 |
|||
DATA "Given$a$text$file$of$many$lines,$where$fields$within$a$line$" |
|||
DATA "are$delineated$by$a$single$'dollar'$character,$write$a$program" |
|||
DATA "that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$" |
|||
DATA "column$are$separated$by$at$least$one$space." |
|||
DATA "Further,$allow$for$each$word$in$a$column$to$be$either$left$" |
|||
DATA "justified,$right$justified,$or$center$justified$within$its$column." |
|||
REM First find the maximum length of a 'word': |
|||
max% = 0 |
|||
READ nlines% |
|||
FOR Line% = 1 TO nlines% |
|||
READ text$ |
|||
REPEAT |
|||
word$ = FNword(text$, "$") |
|||
IF LEN(word$) > max% THEN max% = LEN(word$) |
|||
UNTIL word$ = "" |
|||
NEXT Line% |
|||
@% = max% : REM set column width |
|||
REM Now display the aligned text: |
|||
RESTORE |
|||
READ nlines% |
|||
FOR Line% = 1 TO nlines% |
|||
READ text$ |
|||
REPEAT |
|||
word$ = FNword(text$, "$") |
|||
PRINT FNjustify(word$, max%, "left"),; |
|||
UNTIL word$ = "" |
|||
PRINT |
|||
NEXT Line% |
|||
END |
|||
DEF FNword(text$, delim$) |
|||
PRIVATE delim% |
|||
LOCAL previous% |
|||
IF delim% = 0 THEN |
|||
previous% = 1 |
|||
ELSE |
|||
previous% = delim% + LEN(delim$) |
|||
ENDIF |
|||
delim% = INSTR(text$+delim$, delim$, previous%) |
|||
IF delim% = 0 THEN |
|||
= "" |
|||
ELSE |
|||
= MID$(text$, previous%, delim%-previous%) + " " |
|||
ENDIF |
|||
DEF FNjustify(word$, field%, mode$) |
|||
IF word$ = "" THEN = "" |
|||
CASE mode$ OF |
|||
WHEN "center": = STRING$((field%-LEN(word$)) DIV 2, " ") + word$ |
|||
WHEN "right": = STRING$(field%-LEN(word$), " ") + word$ |
|||
ENDCASE |
|||
= word$</lang> |
|||
=={{header|C}}== |
=={{header|C}}== |