lining up delimiter separated text

S

scooterm

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
 
J

James Gray

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.

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.size }.max }

data.inject("") do |output, row|
output +
(0...row.size).map { |i| "%-#{sizes}s" % row }.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 Gray II
 
X

Xavier Noria

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.

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
 
7

7stud --

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
 
7

7stud --

7stud said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top