Lining up delimiter separated text

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

On Jan 28, 2008, at 11:44 AM, [email protected] wrote:

bart ;; simpson ;; 10 ;; male

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

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

#!/usr/bin/env ruby -wKU

def pretty_print(input, sep = ‘;;’)
data = input.map { |line| line.chomp.split(/
\s*#{Regexp.escape(sep)}\s*/) }
sizes = (0…data.first.size).map { |i| data.map { |row|
row[i].size }.max }

data.inject("") do |output, row|
output +
(0…row.size).map { |i| “%-#{sizes[i]}s” % row[i] }.join("
#{sep} ") + “\n”
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 G. II

On Jan 28, 2008, at 6:44 PM, [email protected] wrote:

bart ;; simpson ;; 10 ;; male

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

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|
format = max_widths[0…r.size].map {|mw| “%-#{mw}s”}.join(" ;; ")
puts format % r
end

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

7stud – wrote:

[email protected] 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|
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|
puts “%-8s%-4s%-8s%-4s%-4s%-4s%-8s” % line.chomp.split(/\s*(;;)\s*/)
end

[email protected] 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|
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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs