Align columns: Difference between revisions

no edit summary
m (→‎{{header|C sharp}}: Regularize header markup to recommended on category page)
No edit summary
Line 1,258:
END</lang>
 
=={{header|Amazing Hopper}}==
<lang Amazing Hopper>
#define IGet(__N__,__X__) [__N__]SGet(__X__)
 
#include <hbasic.h>
#define MAX_LINE 1000
 
Begin
Option Stack 15
Declare as Numeric ( fd, i, index, max token )
as Numeric ( num tokens, size Column, tCells, nMaxValue )
as Alpha ( line )
as Numeric ( display Left, display Right, display Center )
as Stack ( maxSize )
GetParam(script, file to edit, separator)
 
// get statistics of file: #lines, #total chars, line more long, and num tokens from first line.
Token Sep( separator )
Stat( file to edit, dats )
// declare arrays to work:
Dim ( IGet(1,dats), Add(IGet(4,dats),10) ) for Fill Array("",cells)
Clear(dats)
MStore ( cells, display Left, display Right, display Center )
// read each line as array, get # of elements, and put into array cells:
Open(OPEN_READ, file to edit ) (fd)
When( File Error ){ Stop }
 
index=1, max token = 0
While Not Eof(fd)
Take ( ReadRow(MAX_LINE)(fd) » (line) ), get Length, and Move to (num tokens)
Set Interval [index, 1:num tokens]; Take( line ) and SPut(cells)
When ( var( max token) Is Lt (num tokens) ) { max token = num tokens }
++index
Wend
Close(fd)
Token Sep (",")
 
// formatting...
For Up( i:=1, max token, 1 )
Set Interval [1:end,i], and Let ( nMaxValue := MaxValue( Len(SGet(cells)) ) Plus(1) )
Take ( nMaxValue ), and Push ( maxSize )
Next
 
For Up( i:=1, max token, 1 )
Let ( size Column := QPop(maxSize) )
Let ( tCells := [1:end,i]Get(cells) )
LPad$( " ", size Column, tCells ), and Put(display Left)
RPad$( " ", size Column, tCells ), and Put(display Right)
CPad$( " ", size Column, tCells ), and Put(display Center)
Next
// display:
Token Sep ("")
Print("Left:\n", display Left, Newl, "Right:\n", display Right, Newl, "Center:\n", display Center,Newl)
End
</lang>
{{out}}
Sample String was saved into "sample.txt" file.
<pre>
$ hopper src/acolumns.bas sample.txt "$"
Left:
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.
 
Right:
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.
 
Center:
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.
$
</pre>
=={{header|AppleScript}}==
 
543

edits