How to resolve the algorithm Align columns step by step in the Vedit macro language programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Align columns step by step in the Vedit macro language programming language

Table of Contents

Problem Statement

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. Use the following text to test your programs:

Note that:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Align columns step by step in the Vedit macro language programming language

Source code in the vedit programming language

RS(10, "$")		// Field separator
#11 = 1			// Align: 1 = left, 2 = center, 3 = right

// Reset column widths. Max 50 columns
for (#1=40; #1<90; #1++) { #@1 = 0 }

// Find max width of each column
BOF
Repeat(ALL) {
    for (#1=40; #1<90; #1++) {
        Match(@10, ADVANCE)			// skip field separator if any
	#2 = Cur_Pos
	Search("|{|@(10),|N}", NOERR)		// field separator or end of line
	#3 = Cur_Pos - #2			// width of text
	if (#3 > #@1) { #@1 = #3 }
	if (At_EOL) { Break }
    }
    Line(1, ERRBREAK)
}

// Convert lines
BOF
Repeat(ALL) {
    for (#1=40; #1<90; #1++) {
	#2 = Cur_Pos
	Search("|{|@(10),|N}", NOERR)
	if (At_EOL==0) { Del_Char(Chars_Matched) }
	#3 = #@1 - Cur_Pos + #2			// number of spaces to insert
	#4 = 0
	if (#11 == 2) { #4 = #3/2; #3 -= #4 }	// Center
	if (#11 == 3) { #4 = #3;   #3 = 0 }	// Right justify
	Set_Marker(1, Cur_Pos)
	Goto_Pos(#2)
	Ins_Char(' ', COUNT, #4)		// add spaces before the word
	Goto_Pos(Marker(1))
	Ins_Char(' ', COUNT, #3+1)		// add spaces after the word
	if (At_EOL) { Break }
    }
    Line(1, ERRBREAK)
}

  

You may also check:How to resolve the algorithm Arena storage pool step by step in the Raku programming language
You may also check:How to resolve the algorithm Roots of a quadratic function step by step in the Perl programming language
You may also check:How to resolve the algorithm Loops/With multiple ranges step by step in the AWK programming language
You may also check:How to resolve the algorithm Truncate a file step by step in the Java programming language
You may also check:How to resolve the algorithm Leap year step by step in the Vedit macro language programming language