# Align columns

```ruby
class Format(text, width) {
    method align(j) {
        text.map { |row|
            row.range.map { |i|
                '%-*s ' % (width[i],
                  '%*s' % (row[i].len + (width[i]-row[i].len * j/2), row[i]));
            }.join("");
        }.join("\n") + "\n";
    }
}
 
func Formatter(text) {
    var textArr = [];
    var widthArr = [];
 
    text.each_line {
        var words = .split('$');
        textArr.append(words);
 
        words.each_kv { |i, word|
            if (i == widthArr.len) {
                widthArr.append(word.len);
            }
            elsif (word.len > widthArr[i]) {
                widthArr[i] = word.len;
            }
        }
    }
 
    return Format(textArr, widthArr);
}
 
enum |left, middle, right|;
const text = <<'EOT';
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.
EOT
 
var f = Formatter(text);
 
say f.align(left);
say f.align(middle);
say f.align(right);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://trizen.gitbook.io/sidef-lang/programming_tasks/a/align_columns.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
