Align columns: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 30:
=={{header|11l}}==
{{trans|D}}
<syntaxhighlight lang="11l">V txt = ‘Given$a$txt$file$of$many$lines,$where$fields$within$a$line$
are$delineated$by$a$single$'dollar'$character,$write$a$program
that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$
Line 56:
 
=={{header|360 Assembly}}==
<syntaxhighlight lang="360asm">* Align columns 12/04/2019
ALICOL CSECT
USING ALICOL,R13 base register
Line 262:
 
=={{header|8th}}==
<syntaxhighlight lang=Forth"forth">
quote | Given$a$text$file$of$many$lines,$where$fields$within$a$line$
are$delineated$by$a$single$'dollar'$character,$write$a$program
Line 337:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang=AArch64"aarch64 Assemblyassembly">
/* ARM assembly AARCH64 Raspberry PI 3B or android 64 bits */
/* program alignColumn64.s */
Line 693:
</pre>
=={{header|ABAP}}==
<syntaxhighlight lang=ABAP"abap">report z_align no standard page header.
start-of-selection.
 
Line 794:
=={{header|Action!}}==
Atari 8-bit computer is able to show only 40 characters per line. The user has to press left/right arrow keys to scroll the content of whole text.
<syntaxhighlight lang=Action"action!">DEFINE LINES_COUNT="10"
DEFINE COLUMNS_COUNT="20"
DEFINE WORDS_COUNT="100"
Line 1,032:
=={{header|Ada}}==
{{libheader|Simple components for Ada}}
<syntaxhighlight lang="ada">with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
with Ada.Text_IO; use Ada.Text_IO;
with Strings_Edit; use Strings_Edit;
Line 1,120:
 
=={{header|Aime}}==
<syntaxhighlight lang="aime">data b;
file f;
text n, t;
Line 1,191:
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">STRING nl = REPR 10;
STRING text in list := "Given$a$text$file$of$many$lines,$where$fields$within$a$line$"+nl+
"are$delineated$by$a$single$'dollar'$character,$write$a$program"+nl+
Line 1,259:
 
=={{header|Amazing Hopper}}==
<syntaxhighlight lang=Amazing"amazing Hopperhopper">
#define IGet(__N__,__X__) [__N__]SGet(__X__)
 
Line 1,346:
 
{{trans|JavaScript}}
<syntaxhighlight lang=AppleScript"applescript">-- COLUMN ALIGNMENTS ---------------------------------------------------------
 
property pstrLines : ¬
Line 1,621:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang=ARM"arm Assemblyassembly">
/* ARM assembly Raspberry PI */
/* program alignColumn.s */
Line 1,974:
</pre>
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">text: {
Given$a$text$file$of$many$lines,$where$fields$within$a$line$
are$delineated$by$a$single$'dollar'$character,$write$a$program
Line 2,001:
=={{header|AutoHotkey}}==
 
<syntaxhighlight lang=AutoHotkey"autohotkey">Alignment := "L" ; Options: L, R, C
Text =
( LTrim
Line 2,052:
 
=={{header|AutoIt}}==
<syntaxhighlight lang=AutoIt"autoit">
; == If the given text is in an file, it will read with:
#include <File.au3>
Line 2,148:
 
=={{header|AWK}}==
<syntaxhighlight lang=AWK"awk">
# syntax: GAWK -f ALIGN_COLUMNS.AWK ALIGN_COLUMNS.TXT
BEGIN {
Line 2,227:
 
=={{header|BaCon}}==
<syntaxhighlight lang="freebasic">
DECLARE in$[] = { "Given$a$text$file$of$many$lines,$where$fields$within$a$line$", \
"are$delineated$by$a$single$'dollar'$character,$write$a$program", \
Line 2,292:
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<syntaxhighlight 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"
Line 2,350:
 
==={{header|Commodore BASIC}}===
<syntaxhighlight lang="basic">10 rem ********************************
20 rem print words in columns
30 rem commodore basic 2.0
Line 2,418:
 
=={{header|Batch File}}==
<syntaxhighlight lang="dos">@echo off
setlocal enabledelayedexpansion
mode con cols=103
Line 2,525:
 
=={{header|Beads}}==
<syntaxhighlight lang="beads">beads 1 program 'Align columns'
const
Line 2,629:
=={{header|BQN}}==
A function which returns a rectangular 2D array which represents the lines.
<syntaxhighlight lang="bqn">Split ← (⊢-˜+`׬)∘=⊔⊢
PadRow ← {
w‿t𝕊𝕩: # t → type.
Line 2,641:
 
1 Align text</syntaxhighlight>
<syntaxhighlight lang="bqn">┌─
╵" Given a text file of many lines, where fields within a line
are delineated by a single 'dollar' character, write a program
Line 2,660:
{{works with|C sharp|C#|2+}}
 
<syntaxhighlight lang="csharp">using System;
class ColumnAlignerProgram
{
Line 2,739:
 
{{works with|C sharp|C#|8+}}
<syntaxhighlight lang="csharp">using System;
using System.Linq;
 
Line 2,821:
 
=={{header|Clojure}}==
<syntaxhighlight lang=Clojure"clojure">
(ns rosettacode.align-columns
(:require [clojure.contrib.string :as str]))
Line 2,866:
 
=={{header|COBOL}}==
<syntaxhighlight lang="cobol">
identification division.
program-id. AlignColumns.
Line 2,952:
</syntaxhighlight>
{{out}}
<syntaxhighlight lang="text">
------------------------------------------------------------------------------------------------------------------------
Given a text file of many lines, where fields within a line
Line 2,977:
 
=={{header|CoffeeScript}}==
<syntaxhighlight lang="coffeescript">
pad = (n) ->
s = ''
Line 3,028:
</syntaxhighlight>
{{out}}
<syntaxhighlight lang="text">
> coffee align_columns.coffee
 
Line 3,057:
 
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">(defun nonempty (seq)
(position-if (lambda (x) (declare (ignore x)) t) seq))
 
Line 3,107:
 
=={{header|D}}==
<syntaxhighlight lang="d">void main() {
import std.stdio, std.string, std.algorithm, std.range, std.typetuple;
 
Line 3,151:
=={{header|Delphi}}==
{{libheader|Delphi StdCtrls, Classes, SysUtils, StrUtils, Contnrs}}
<syntaxhighlight lang=Delphi"delphi">
USES
StdCtrls, Classes, SysUtils, StrUtils, Contnrs;
Line 3,212:
=={{header|E}}==
 
<syntaxhighlight lang="e">pragma.enable("accumulator")
 
def left(width, word) {
Line 3,242:
}</syntaxhighlight>
 
<syntaxhighlight lang="e">? def text := "Given$a$text$file$of$many$lines,$where$fields$within$a$line$
are$delineated$by$a$single$'dollar'$character,$write$a$program
that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$
Line 3,278:
The String module of Elixir doesn't have the function of the center position adjusting.
It calls and processes the function of 'Erlang'.
<syntaxhighlight lang="elixir">defmodule Align do
def columns(text, alignment) do
fieldsbyrow = String.split(text, "\n", trim: true)
Line 3,342:
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
-module (align_columns).
Line 3,413:
 
=={{header|Euphoria}}==
<syntaxhighlight lang="euphoria">constant data = {
"Given$a$text$file$of$many$lines,$where$fields$within$a$line$",
"are$delineated$by$a$single$'dollar'$character,$write$a$program",
Line 3,500:
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">open System
open System.IO
 
Line 3,557:
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: fry io kernel math math.functions math.order sequences
splitting strings ;
IN: rosetta.column-aligner
Line 3,602:
=={{header|FBSL}}==
Using a multiline string:
<syntaxhighlight lang="qbasic">#APPTYPE CONSOLE
 
DIM s = "Given$a$text$file$of$many$lines,$where$fields$within$a$line$
Line 3,649:
=={{header|Forth}}==
{{works with|GNU Forth}}
<syntaxhighlight lang="forth">\ align columns
 
: split ( addr len char -- addr len1 addr len-len1 )
Line 3,736:
 
Given a FORMAT text that produces output in aligned columns makes it easy enough to meet the other requirements. To cause texts to be aligned left, append sufficient spaces to each output text, and for centred text, half that number. This relies on the special intrinsic function REPEAT(text,n) returning a varying number of characters - CHARACTER functions have to return a ''fixed'' number of characters, until the standardisation of varying-length strings in F2003 ''et seq''. Earlier Fortrans lack the REPEAT function, but its effect can be gained via something like CHARACTER*66 SPACE, where SPACE is set to spaces, and SPACE(1:N) is used where REPEAT(" ",N) is desired. And if messing with variable FORMAT is unwanted, the REPEAT scheme can be used for the right-justified output also.
<syntaxhighlight lang=Fortran"fortran">
SUBROUTINE RAKE(IN,M,X,WAY) !Casts forth text in fixed-width columns.
Collates column widths so that each column is wide enough for its widest member.
Line 3,865:
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Sub Split(s As String, sep As String, result() As String)
Line 4,005:
=={{header|FutureBasic}}==
Note: FB's NSLog, used for demonstrations, does not have a center alignment provision. However FB allows advanced alignment and text formatting in normal application development. Simple right and left alignment are shown here.
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
Line 4,081:
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=023b4c5144d45e047abe02ebf5c4525a Click this link to run this code]'''
<syntaxhighlight lang="gambas">Public Sub Main() 'Written in Gambas 3.9.2 as a Command line Application - 15/03/2017
Dim siCount, siCounter, siLength As Short 'Counters
Dim siLongest As Short = -1 'To store the longest 'Word'
Line 4,151:
 
=={{header|Go}}==
<syntaxhighlight lang="go">package main
 
import (
Line 4,237:
=={{header|Groovy}}==
Solution:
<syntaxhighlight lang="groovy">def alignColumns = { align, rawText ->
def lines = rawText.tokenize('\n')
def words = lines.collect { it.tokenize(/\$/) }
Line 4,253:
 
Test Program:
<syntaxhighlight lang="groovy">def rawTextInput = '''Given$a$text$file$of$many$lines,$where$fields$within$a$line$
are$delineated$by$a$single$'dollar'$character,$write$a$program
that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$
Line 4,293:
=={{header|Harbour}}==
 
<syntaxhighlight lang="visualfoxpro">
PROCEDURE Main()
LOCAL a := { "Given$a$text$file$of$many$lines,$where$fields$within$a$line$",;
Line 4,336:
</syntaxhighlight>
Output:
<syntaxhighlight lang="text">----Left aligned columns----
Given a text file of many lines, where fields within a line
are delineated by a single 'dollar' character, write a program
Line 4,361:
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">import Data.List (unfoldr, transpose)
import Control.Arrow (second)
 
Line 4,400:
Or, using '''Text''' and its functions as an alternative to '''[Char]''' strings:
 
<syntaxhighlight lang="haskell">import Prelude as P
import Data.Text as T
(Text, pack, unpack, splitOn, unlines, unwords, length,
Line 4,459:
=={{header|HicEst}}==
A file opened with a Format option describing the column format(s) can be addressed like a standard in-memory array. In addition the DLG function ([http://www.HicEst.com/MatrixExplorer.htm MatrixExplorer]) allows this text/numeric file to be edited or visualized in many ways, but string columns are always left adjusted while numeric columns are right adjusted. Export is possible.
<syntaxhighlight lang=HicEst"hicest">
CHARACTER Fnam = "\HicEst\Rosetta\Align columns.txt"
 
Line 4,509:
An argument of <tt>left</tt>, <tt>center</tt>, or <tt>right</tt> controls the
column alignment. The default is left-alignment.
<syntaxhighlight lang="icon">global width
 
procedure main(args)
Line 4,557:
 
'''Solution'''
<syntaxhighlight lang="j">'LEFT CENTER RIGHT'=: i.3 NB. justification constants
NB.* alignCols v Format delimited text in justified columns
Line 4,577:
 
'''Example''':
<syntaxhighlight lang="j"> text=: noun define
Given$a$text$file$of$many$lines,$where$fields$within$a$line$
are$delineated$by$a$single$'dollar'$character,$write$a$program
Line 4,607:
{{libheader|Apache Commons Lang}}
 
<syntaxhighlight lang=Java"java">import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Line 4,756:
===Imperative===
 
<syntaxhighlight lang=JavaScript"javascript">
var justification="center",
input=["Given$a$text$file$of$many$lines,$where$fields$within$a$line$",
Line 4,791:
===Functional===
 
<syntaxhighlight lang=JavaScript"javascript">//break up each string by '$'. The assumption is that the user wants the trailing $.
var data = [
"Given$a$text$file$of$many$lines,$where$fields$within$a$line$",
Line 4,844:
Or (ES5) using transpose and zipWith:
 
<syntaxhighlight lang=JavaScript"javascript">(function (strText) {
'use strict';
Line 5,004:
{{ Works with|jq|1.4}}
The key to the following implementation is the filter named "transpose", which is defined to work on a possibly jagged matrix. It is provided as a built-in in jq 1.5 and later.
<syntaxhighlight lang="jq"># transpose a possibly jagged matrix
def transpose:
if . == [] then []
Line 5,048:
;</syntaxhighlight>
'''Example''':
<syntaxhighlight lang="jq">"Center:", format("center"), "",
"Left:", format("left"), "",
"Right:", format("right")</syntaxhighlight>
{{Out}}
<div style="overflow:scroll; height:200px;"><syntaxhighlight lang="sh">$ jq -M -R -r -s -f Align_columns.jq Align_columns.txt
Center:
Given a text file of many lines, where fields within a line
Line 5,080:
=={{header|Jsish}}==
From Javascript ES5 entry.
<syntaxhighlight lang="javascript">/* Align columns, in Jsish */
function alignColumns(phrases:array, just:string) {
var x, y, max, diff, left, right, cols=0;
Line 5,151:
=={{header|Julia}}==
{{trans|Python}}
<syntaxhighlight lang="julia">txt = """Given\$a\$txt\$file\$of\$many\$lines,\$where\$fields\$within\$a\$line\$
are\$delineated\$by\$a\$single\$'dollar'\$character,\$write\$a\$program
that\$aligns\$each\$column\$of\$fields\$by\$ensuring\$that\$words\$in\$each\$
Line 5,220:
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.Paths
Line 5,288:
The input data is a sequence of characters where words are supposed to be separated by $ and lines be ended by \$. A HTML table is used to format the output left, centered or right justified.
 
<syntaxhighlight lang="scheme">
{def txt
Given$a$text$file$of$many$lines,$where$fields$within$a$line\$are$delineated$by$a$single$'dollar'$character,$write$a$program\$that$aligns$each$column$of$fields$by$ensuring$that$words$in$each\$column$are$separated$by$at$least$one$space.\$Further,$allow$for$each$word$in$a$column$to$be$either$left\$justified,$right$justified,$or$center$justified$within$its$column.}
Line 5,330:
 
=={{header|Lasso}}==
<syntaxhighlight lang=Lasso"lasso">#!/usr/bin/lasso9
 
local(text = "Given$a$text$file$of$many$lines,$where$fields$within$a$line$
Line 5,427:
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">mainwin 140 32
 
CRLF$ =chr$( 13)
Line 5,509:
{{works with|Lua|5.1}}
 
<syntaxhighlight lang="lua">
local tWord = {} -- word table
local tColLen = {} -- maximum word length in a column
Line 5,574:
Usage Example:
 
<syntaxhighlight lang="lua">
input =
[[Given$a$text$file$of$many$lines,$where$fields$within$a$line$
Line 5,590:
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang=M2000"m2000 Interpreterinterpreter">
Module Align_Columns {
a$={Given$a$text$file$of$many$lines,$where$fields$within$a$line$
Line 5,669:
=={{header|Maple}}==
Assign the sample data.
<syntaxhighlight lang=Maple"maple">
txt :=
"Given$a$text$file$of$many$lines,$where$fields$within$a$line$\n"
Line 5,679:
</syntaxhighlight>
The following procedure solves the problem. It takes the string to be operated on as input, and an optional alignment parameter, which defaults to centred alignment. The aligned text is returned, as a string, which can then be printed.
<syntaxhighlight lang=Maple"maple">
AlignColumns := proc( txt, align :: { "left", "right", "centre" } := "centre" )
uses StringTools;
Line 5,706:
</syntaxhighlight>
For the sample text, we get the following results.
<syntaxhighlight lang=Maple"maple">
> printf( "%s\n", AlignColumns( txt ) ):
Given a text file of many lines, where fields within a line
Line 5,739:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">TableForm[StringSplit[StringSplit[a,"\n"],"$"],TableAlignments -> Center]</syntaxhighlight>
Output with example text :
[[File:centeredtext.png]]
Line 5,745:
=={{header|MATLAB}} / {{header|Octave}}==
center-justified formatting is not implemented here
<syntaxhighlight lang=Matlab"matlab">
function r = align_columns(f)
fid = fopen('align_column_data.txt', 'r');
Line 5,801:
Note the presetting of P102 to indicate the alignment required.
 
<syntaxhighlight lang=ML"ml/Ii">MCSKIP "WITH" NL
"" Align columns - assumes macros on input stream 1, data on stream 2
MCPVAR 102
Line 5,863:
Note the presetting of P102 to indicate the alignment required.
 
<syntaxhighlight lang=ML"ml/Ii">MCSKIP "WITH" NL
"" Align columns - assumes macros on input stream 1, data on stream 2
MCPVAR 102
Line 5,922:
 
=={{header|MUMPS}}==
<syntaxhighlight lang=MUMPS"mumps">columns(how) ; how = "Left", "Center" or "Right"
New col,half,ii,max,spaces,word
Set ii=0
Line 5,977:
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">from strutils import splitLines, split
from sequtils import mapIt
from strfmt import format, write
Line 6,031:
Source: [https://github.com/nitlang/nit/blob/master/examples/rosettacode/align_columns.nit the official Nit’s repository]
 
<syntaxhighlight lang="nit"># Task: Align columns
#
# Uses `Text::justify` from the standard library.
Line 6,103:
=={{header|Oberon-2}}==
works with oo2c version 2.
<syntaxhighlight lang="oberon2">
MODULE Columns;
IMPORT
Line 6,239:
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">#load "str.cma"
open Str
 
Line 6,296:
=={{header|Oforth}}==
 
<syntaxhighlight lang="oforth">import: mapping
import: file
 
Line 6,332:
 
=={{header|ooRexx}}==
<syntaxhighlight lang=ooRexx"oorexx">
text = .array~of("Given$a$text$file$of$many$lines,$where$fields$within$a$line$", -
"are$delineated$by$a$single$'dollar'$character,$write$a$program", -
Line 6,438:
 
=={{header|OpenEdge/Progress}}==
<syntaxhighlight lang="progress">FUNCTION alignColumns RETURNS CHAR (
i_c AS CHAR,
i_calign AS CHAR
Line 6,662:
 
=={{header|Oz}}==
<syntaxhighlight lang="oz">declare
%% Lines: list of strings
%% Alignment: function like fun {Left Txt ExtraSpace} ... end
Line 6,741:
See [[Align_columns#Delphi | Delphi]]
modified to {{works with|Free Pascal}}
<syntaxhighlight lang="pascal">program Project1;
 
{$H+}//Use ansistrings
Line 6,840:
 
=={{header|Perl}}==
<syntaxhighlight lang=Perl"perl">#/usr/bin/perl -w
use strict ;
 
Line 6,897:
}</syntaxhighlight>
a shorter solution
<syntaxhighlight lang="perl">use List::Util qw(max);
 
sub columns {
Line 6,923:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang=Phix"phix">-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">data</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span>
<span style="color: #008000;">"Given$a$text$file$of$many$lines,$where$fields$within$a$line$"</span><span style="color: #0000FF;">,</span>
Line 7,017:
 
=={{header|Phixmonti}}==
<syntaxhighlight lang=Phixmonti"phixmonti">include ..\Utilitys.pmt
 
0 40 repeat var gap
Line 7,065:
 
=={{header|PHP}}==
<syntaxhighlight lang="php"><?php
$j2justtype = array('L' => STR_PAD_RIGHT,
'R' => STR_PAD_LEFT,
Line 7,114:
 
=={{header|Picat}}==
<syntaxhighlight lang=Picat"picat">import util.
 
main =>
Line 7,188:
 
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp"picolisp">(let Sizes NIL # Build a list of sizes
(let Lines # and of lines
(make
Line 7,232:
 
=={{header|PL/I}}==
<syntaxhighlight lang=PL"pl/Ii">
declare text character (300) varying;
declare word character (20) varying;
Line 7,273:
 
=={{header|PowerShell}}==
<syntaxhighlight lang=PowerShell"powershell">
$file =
@'
Line 7,309:
=={{header|Prolog}}==
Works with SWI-Prolog.
<syntaxhighlight lang=Prolog"prolog">aligner :-
L ="Given$a$text$file$of$many$lines,$where$fields$within$a$line$
are$delineated$by$a$single$'dollar'$character,$write$a$program
Line 7,420:
=={{header|PureBasic}}==
{{works with|PureBasic|4.41}}
<syntaxhighlight lang=PureBasic"purebasic">Declare max(a,b)
 
If OpenConsole()
Line 7,499:
===Procedural===
====Using f-strings====
<syntaxhighlight lang="python">from itertools import zip_longest
 
txt = """Given$a$txt$file$of$many$lines,$where$fields$within$a$line$
Line 7,550:
 
====Using StringIO====
<syntaxhighlight lang="python">from StringIO import StringIO
textinfile = '''Given$a$text$file$of$many$lines,$where$fields$within$a$line$
Line 7,625:
Works with Python 2 and 3.
 
<syntaxhighlight lang="python">'''
cat <<'EOF' > align_columns.dat
Given$a$text$file$of$many$lines,$where$fields$within$a$line$
Line 7,647:
====Alternative====
{{trans|D}}
<syntaxhighlight lang="python">txt = """Given$a$txt$file$of$many$lines,$where$fields$within$a$line$
are$delineated$by$a$single$'dollar'$character,$write$a$program
that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$
Line 7,675:
(Selection of string justification methods via '''getattr'''):
{{Works with|Python|3.7}}
<syntaxhighlight lang="python">'''Variously aligned columns
from delimited text.
'''
Line 7,773:
 
=={{header|q}}==
<syntaxhighlight lang="q">text:(
"Given$a$text$file$of$many$lines,$where$fields$within$a$line$";
"are$delineated$by$a$single$'dollar'$character,$write$a$program";
Line 7,814:
 
=={{header|R}}==
<syntaxhighlight lang=R"r"># Read in text
lines <- readLines(tc <- textConnection("Given$a$text$file$of$many$lines,$where$fields$within$a$line$
are$delineated$by$a$single$'dollar'$character,$write$a$program
Line 7,850:
 
=={{header|Racket}}==
<syntaxhighlight lang=Racket"racket">
#lang racket
 
Line 7,893:
Call with parameter left (default), center or right.
 
<syntaxhighlight lang=perl6"raku" line>my @lines =
q|Given$a$text$file$of$many$lines,$where$fields$within$a$line$
are$delineated$by$a$single$'dollar'$character,$write$a$program
Line 7,919:
Or a more functional version, called like <code>./align.p6 left input.txt</code>, which however only supports left and right alignment (not center):
 
<syntaxhighlight lang=perl6"raku" line>sub MAIN ($alignment where 'left'|'right', $file) {
my @lines := $file.IO.lines.map(*.split('$').cache).cache;
my @widths = roundrobin(|@lines).map(*».chars.max);
Line 7,928:
 
=={{header|RapidQ}}==
<syntaxhighlight lang="vb">
Dim MText as QMemorystream
MText.WriteLine "Given$a$text$file$of$many$lines,$where$fields$within$a$line$"
Line 7,998:
 
=={{header|REBOL}}==
<syntaxhighlight lang="rebol">REBOL [
Title: "Align Columns"
URL: http://rosettacode.org/wiki/Align_columns
Line 8,087:
 
=={{header|Red}}==
<syntaxhighlight lang=Red"red">Red [
Title: "Align Columns"
Original-Author: oofoe
Line 8,149:
=={{header|REXX}}==
===(no output)===
<syntaxhighlight lang="rexx">/*REXX*/
z.1 = "Given$a$text$file$of$many$lines,$where$fields$within$a$line$"
z.2 = "are$delineated$by$a$single$'dollar'$character,$write$a$program"
Line 8,200:
 
===(with output)===
<syntaxhighlight lang="rexx">/*REXX program displays various alignments for words in an array of text strings. */
size= 0; t.=; cols= 0; wid.= 0; @.= /*zero or nullify some variables. */
t.1 = "Given$a$text$file$of$many$lines,$where$fields$within$a$line$"
Line 8,261:
===(boxed output)===
Note: &nbsp; this version boxes each column of output to better show the columns.
<syntaxhighlight lang="rexx">/*REXX program displays various alignments for words in an array of text strings. */
size= 0; t.=; cols= 0; wid.= 0; @.= /*zero or nullify some variables. */
t.1 = "Given$a$text$file$of$many$lines,$where$fields$within$a$line$"
Line 8,335:
=={{header|Ruby}}==
{{works with|Ruby|1.9.3+}}
<syntaxhighlight lang="ruby">J2justifier = {Left: :ljust, Right: :rjust, Center: :center}
 
=begin
Line 8,408:
 
=={{header|Run BASIC}}==
<syntaxhighlight lang=Runbasic"runbasic">theString$ = "Given$a$text$file$of$many$lines,$where$fields$within$a$line$" _
+ "are$delineated$by$a$single$'dollar'$character,$write$a$program" _
+ "that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$"_
Line 8,486:
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">use std::iter::{repeat, Extend};
 
enum AlignmentType {
Line 8,581:
For Scala 2.7, change from fromPath to fromFile, and remove the extra parameter to Source's getLines.
 
<syntaxhighlight lang="scala">object ColumnAligner {
val eol = System.getProperty("line.separator")
def getLines(filename: String) = scala.io.Source.fromPath(filename).getLines(eol)
Line 8,612:
Another take:
 
<syntaxhighlight lang="scala">def pad(s:String, i:Int, d:String) = {
val padsize = (i-s.length).max(0)
d match {
Line 8,637:
=={{header|Scheme}}==
 
<syntaxhighlight lang="scheme">
(import (scheme base)
(scheme write)
Line 8,723:
=={{header|sed}}==
The code allows to left (by default) or right justify colums. Centering is not supported. Requires about 2x<size of input> bytes of memory (each line duplicated).
<syntaxhighlight lang="sed">
#!/bin/sed -nrf
# Format: <master-pattern>\n<line1>\n<line1-as-pattern>\n<line2>\n<line2-as-pattern>...
Line 8,819:
 
=={{header|Seed7}}==
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const array string: inputLines is [] (
Line 8,883:
 
=={{header|Shiny}}==
<syntaxhighlight lang="shiny">text: 'Given$a$text$file$of$many$lines,$where$fields$within$a$line$
are$delineated$by$a$single$\'dollar\'$character,$write$a$program
that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$
Line 8,945:
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">class Format(text, width) {
method align(j) {
text.map { |row|
Line 8,996:
the following works with Smalltalk/X, but should also in other dialects (may have to add "centerPadded" to the String class).
{{works with |Smalltalk/X }}
<syntaxhighlight lang="smalltalk">text :=
'Given$a$text$file$of$many$lines,$where$fields$within$a$line$
are$delineated$by$a$single$''dollar''$character,$write$a$program
Line 9,138:
=={{header|Snobol}}==
{{works with|Snobol|4}}
<syntaxhighlight lang="snobol">* Since we don't know how much text we'll be reading in,
* we store the words and field widths in tables
Words = TABLE()
Line 9,259:
 
=={{header|Standard ML}}==
<syntaxhighlight lang="sml">fun curry f x y = f (x, y)
fun uncurry f (x, y) = f x y
 
Line 9,315:
{{trans|Rust}}
 
<syntaxhighlight lang="swift">import Foundation
 
extension String {
Line 9,428:
 
=={{header|Tcl}}==
<syntaxhighlight lang="tcl">package require Tcl 8.5
 
set text {Given$a$text$file$of$many$lines,$where$fields$within$a$line$
Line 9,501:
Transd has built-in support for "left" and "right" formatting, but not "centered", which added quite a bit of code.
 
<syntaxhighlight lang="scheme">#lang transd
 
MainModule : {
Line 9,568:
 
=={{header|TSE SAL}}==
<syntaxhighlight lang=TSESAL"tsesal">
INTEGER PROC FNBlockChangeColumnAlignLeftB( INTEGER columnTotalI, INTEGER spaceTotalI, INTEGER buffer1I )
INTEGER B = FALSE
Line 9,713:
 
=={{header|TUSCRIPT}}==
<syntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
MODE DATA
Line 9,750:
=={{header|TXR}}==
 
<syntaxhighlight lang="txr">@(collect)
@ (coll)@{item /[^$]+/}@(end)
@(end)
Line 9,800:
=={{header|UNIX Shell}}==
This is a draft implementation of the "align columns" problem using Unix shell commands. The key tool for left and right justified text is the "rs" command. Centered text is a little more complex, since this is not a feature currently in "rs" (''The centered solution will be added later.'')
<syntaxhighlight lang="bash">
cat <<EOF_OUTER > just-nocenter.sh
#!/bin/sh
Line 9,840:
</syntaxhighlight>
{{out}}
<syntaxhighlight lang="sh">
$ ./just-nocenter.sh
Given a text file of many lines, where fields within a line
Line 9,862:
Note that the left-justified case can be handled trivially by the <tt>column</tt> command, which ships with modern Linux and macOS systems:
 
<syntaxhighlight lang="sh">tr '$' ' ' | column -t</syntaxhighlight>
 
{{Out}}
Line 9,879:
For right justification, each word's string of trailing blanks is moved to the beginning,
and for center justification, the trailing blanks are divided equally between the beginning and end of each word.
<syntaxhighlight lang=Ursala"ursala">#import std
 
text =
Line 9,927:
Both arguments are optional and default to "left" and 1 respectively.
 
<syntaxhighlight lang="vb">
Public Sub TestSplit(Optional align As String = "left", Optional spacing As Integer = 1)
Dim word() As String
Line 10,017:
=={{header|VBScript}}==
{{trans|Rexx}}
<syntaxhighlight lang="vb">' Align columns - RC - VBScript
Const nr=16, nc=16
ReDim d(nc),t(nr), wor(nr,nc)
Line 10,109:
=={{header|Vedit macro language}}==
This implementation converts the file currently being edited. The file can then be saved with different filename if required.
<syntaxhighlight lang="vedit">RS(10, "$") // Field separator
#11 = 1 // Align: 1 = left, 2 = center, 3 = right
 
Line 10,176:
 
=={{header|Visual Basic}}==
<syntaxhighlight lang="vb">Sub AlignCols(Lines, Optional Align As AlignmentConstants, Optional Sep$ = "$", Optional Sp% = 1)
Dim i&, j&, D&, L&, R&: ReDim W(UBound(Lines)): ReDim C&(0)
Line 10,192:
Next i, j
End Sub</syntaxhighlight>
Usage:<syntaxhighlight lang="vb">Sub Main() 'usage of the above
Const Text$ = "Given$a$text$file$of$many$lines,$where$fields$within$a$line$" & vbLf & _
"are$delineated$by$a$single$'dollar'$character,$write$a$program" & vbLf & _
Line 10,230:
 
=={{header|Visual Basic .NET}}==
<syntaxhighlight lang="vb">Module Module1
Private Delegate Function Justification(s As String, width As Integer) As String
 
Line 10,323:
=={{header|Vlang}}==
Currently (5/2022) vlang uses string interpolation so `$` have to be escaped. Variables in formatting aren't currently allowed, and if they were only right/left are currently valid options
<syntaxhighlight lang="vlang">
const text = "Given\$a\$text\$file\$of\$many\$lines,\$where\$fields\$within\$a\$line\$
are\$delineated\$by\$a\$single\$'dollar'\$character,\$write\$a\$program
Line 10,419:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascript">import "io" for File
import "/fmt" for Fmt
 
Line 10,503:
=={{header|Yabasic}}==
{{trans|Run BASIC}}
<syntaxhighlight 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$"
Line 10,574:
 
=={{header|zkl}}==
<syntaxhighlight lang="zkl">fcn format(text,how){
words:=text.split("$").apply("split").flatten();
max:=words.reduce(fcn(p,n){ n=n.len(); n>p and n or p },0);
Line 10,590:
d.text;
}</syntaxhighlight>
<syntaxhighlight lang="zkl">text:=
"Given$a$text$file$of$many$lines,$where$fields$within$a$line$\n"
"are$delineated$by$a$single$'dollar'$character,$write$a$program\n"
Line 10,637:
=={{header|ZX Spectrum Basic}}==
The max width (without 'hack') of ZX Spectrum screen is 32 characters. The text sample is adapted for this feature.
<syntaxhighlight lang="zxbasic"> 5 BORDER 2
10 DATA 6
20 DATA "The$problem$of$Speccy$"
10,327

edits