# Lining up delimiter separated text

**URL:** <https://rubytalk.org/t/lining-up-delimiter-separated-text/44089>\
**Category:** ruby-talk\
**Created:** [28 January 2008 17:44 UTC](https://rubytalk.org/t/lining-up-delimiter-separated-text/44089 "2008-01-28T17:44:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![scooterm](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@scooterm](https://rubytalk.org/u/scooterm)\
**Post date:** [28 January 2008 17:44 UTC](https://rubytalk.org/t/lining-up-delimiter-separated-text/44089/1 "2008-01-28T17:44:56Z")

</div>

BEFORE:  
fname ;; lname ;; age ;; sex ;;  
homer ;; simpson ;; 33 ;; male  
maggie;; simpson ;; 03 ;; female  
bart;;simpson;;10;;male

AFTER:  
fname ;; lname ;; age ;; sex  
homer ;; simpson ;; 33 ;; male  
maggie ;; simpson ;; 03 ;; female  
bart ;; simpson ;; 10 ;; male

REQUEST:  
Show a really cool way to change before into after with  
ruby. Cool = fewest lines OR just plain clever.

--thanks

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [28 January 2008 18:14 UTC](https://rubytalk.org/t/lining-up-delimiter-separated-text/44089/2 "2008-01-28T18:14:07Z")

</div>

This doesn't perfectly match your output, but it gets close:

#!/usr/bin/env ruby -wKU

def pretty\_print(input, sep = ';;')  
&nbsp;&nbsp;&nbsp;data = input.map { |line| line.chomp.split(/\s\*#{Regexp.escape(sep)}\s\*/) }  
&nbsp;&nbsp;&nbsp;sizes = (0...data.first.size).map { |i| data.map { |row| row[i].size }.max }

&nbsp;&nbsp;&nbsp;data.inject("") do |output, row|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(0...row.size).map { |i| "%-#{sizes[i]}s" % row[i] }.join(" #{sep} ") + "\n"  
&nbsp;&nbsp;&nbsp;end  
end

puts pretty\_print(\<\<END\_BEFORE)  
fname ;; lname ;; age ;; sex ;;  
homer ;; simpson ;; 33 ;; male  
maggie;; simpson ;; 03 ;; female  
bart;;simpson;;10;;male  
END\_BEFORE  
# \>\> fname ;; lname ;; age ;; sex  
# \>\> homer ;; simpson ;; 33 ;; male  
# \>\> maggie ;; simpson ;; 03 ;; female  
# \>\> bart ;; simpson ;; 10 ;; male

\_\_END\_\_

James Edward Gray II

> **···**
>
> On Jan 28, 2008, at 11:44 AM, scooterm@hotmail.com wrote:
> 
> > BEFORE:  
> > fname ;; lname ;; age ;; sex ;;  
> > homer ;; simpson ;; 33 ;; male  
> > maggie;; simpson ;; 03 ;; female  
> > bart;;simpson;;10;;male
> > 
> > AFTER:  
> > fname ;; lname ;; age ;; sex  
> > homer ;; simpson ;; 33 ;; male  
> > maggie ;; simpson ;; 03 ;; female  
> > bart ;; simpson ;; 10 ;; male
> > 
> > REQUEST:  
> > Show a really cool way to change before into after with  
> > ruby. Cool = fewest lines OR just plain clever.

---

<div class="post-metadata">

**Author:** ![Xavier\_Noria](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/xavier_noria/32/1867_2.png) [@Xavier\_Noria](https://rubytalk.org/u/Xavier_Noria)\
**Post date:** [28 January 2008 18:15 UTC](https://rubytalk.org/t/lining-up-delimiter-separated-text/44089/3 "2008-01-28T18:15:56Z")

</div>

Modulus whitespace, here's a solution

records = DATA.readlines.map {|line| line.chomp.split(/\s\*;;\s\*/)}  
max\_widths = records.transpose.map {|col| col.map {|cell| cell.length}.max}

records.each do |r|  
&nbsp;&nbsp;&nbsp;format = max\_widths[0...r.size].map {|mw| "%-#{mw}s"}.join(" ;; ")  
&nbsp;&nbsp;&nbsp;puts format % r  
end

\_\_END\_\_  
fname ;; lname ;; age ;; sex ;;  
homer ;; simpson ;; 33 ;; male  
maggie;; simpson ;; 03 ;; female  
bart;;simpson;;10;;male

> **···**
>
> On Jan 28, 2008, at 6:44 PM, scooterm@hotmail.com wrote:
> 
> > BEFORE:  
> > fname ;; lname ;; age ;; sex ;;  
> > homer ;; simpson ;; 33 ;; male  
> > maggie;; simpson ;; 03 ;; female  
> > bart;;simpson;;10;;male
> > 
> > AFTER:  
> > fname ;; lname ;; age ;; sex  
> > homer ;; simpson ;; 33 ;; male  
> > maggie ;; simpson ;; 03 ;; female  
> > bart ;; simpson ;; 10 ;; male
> > 
> > REQUEST:  
> > Show a really cool way to change before into after with  
> > ruby. Cool = fewest lines OR just plain clever.

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [28 January 2008 18:21 UTC](https://rubytalk.org/t/lining-up-delimiter-separated-text/44089/4 "2008-01-28T18:21:39Z")

</div>

scooterm@hotmail.com wrote:

> BEFORE:  
> fname ;; lname ;; age ;; sex ;;  
> homer ;; simpson ;; 33 ;; male  
> maggie;; simpson ;; 03 ;; female  
> bart;;simpson;;10;;male
> 
> AFTER:  
> fname ;; lname ;; age ;; sex  
> homer ;; simpson ;; 33 ;; male  
> maggie ;; simpson ;; 03 ;; female  
> bart ;; simpson ;; 10 ;; male
> 
> REQUEST:  
> Show a really cool way to change before into after with  
> ruby. Cool = fewest lines OR just plain clever.
> 
> --thanks

How about:

str =\<\<STR  
fname ;; lname ;; age ;; sex ;;  
homer ;; simpson ;; 33 ;; male  
maggie;; simpson ;; 03 ;; female  
bart;;simpson;;10;;male  
STR

str.each("\n") do |line|  
&nbsp;&nbsp;puts "%-8s%-4s%-8s%-4s%-4s%-4s%-8s" % line.split(/(;;)/).map {|elmt|  
elmt.strip}  
end

--output:--  
fname ;; lname ;; age ;; sex  
homer ;; simpson ;; 33 ;; male  
maggie ;; simpson ;; 03 ;; female  
bart ;; simpson ;; 10 ;; male

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [28 January 2008 18:29 UTC](https://rubytalk.org/t/lining-up-delimiter-separated-text/44089/5 "2008-01-28T18:29:45Z")

</div>

7stud -- wrote:

> **···**
>
> > scooterm@hotmail.com wrote:
> > 
> > > BEFORE:  
> > > fname ;; lname ;; age ;; sex ;;  
> > > homer ;; simpson ;; 33 ;; male  
> > > maggie;; simpson ;; 03 ;; female  
> > > bart;;simpson;;10;;male
> > > 
> > > AFTER:  
> > > fname ;; lname ;; age ;; sex  
> > > homer ;; simpson ;; 33 ;; male  
> > > maggie ;; simpson ;; 03 ;; female  
> > > bart ;; simpson ;; 10 ;; male
> > > 
> > > REQUEST:  
> > > Show a really cool way to change before into after with  
> > > ruby. Cool = fewest lines OR just plain clever.
> > > 
> > > --thanks
> > 
> > How about:
> > 
> > str =\<\<STR  
> > fname ;; lname ;; age ;; sex ;;  
> > homer ;; simpson ;; 33 ;; male  
> > maggie;; simpson ;; 03 ;; female  
> > bart;;simpson;;10;;male  
> > STR
> > 
> > str.each("\n") do |line|  
> > &nbsp;&nbsp;puts "%-8s%-4s%-8s%-4s%-4s%-4s%-8s" % line.split(/(;;)/).map {|elmt|  
> > elmt.strip}  
> > end
> 
> Or, this might be a little better:
> 
> str.each("\n") do |line|  
> &nbsp;&nbsp;puts "%-8s%-4s%-8s%-4s%-4s%-4s%-8s" % line.chomp.split(/\s\*(;;)\s\*/)  
> end  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
