Align columns: Difference between revisions
Content added Content deleted
(Added Scala) |
|||
Line 1,294: | Line 1,294: | ||
} |
} |
||
}</lang> |
}</lang> |
||
Another take: |
|||
<lang scala> |
|||
def pad(s:String, i:Int, d:String) = { |
|||
val padsize = (i-s.length).max(0) |
|||
d match { |
|||
case "left" => s+" "*padsize |
|||
case "right" => " "*padsize+s |
|||
case "center" => " "*(padsize/2) + s + " "*(padsize-padsize/2) |
|||
} |
|||
} |
|||
val lines = scala.io.Source.fromFile("c:\\text.txt").getLines.map(_.trim()) |
|||
val words = lines.map(_.split("\\$").toList).toList |
|||
val lens = words.map(l => l.map(s=>s.length)).toList |
|||
var maxlens = Map[Int,Int]() withDefaultValue 0 |
|||
lens foreach (l => |
|||
for(i <- (0 until l.length)){ |
|||
maxlens += i -> l(i).max(maxlens(i)) |
|||
} |
|||
) |
|||
val padded = words map { l => l.zipWithIndex.map{case(s,i)=>pad(s,maxlens(i),"left")+" "} } |
|||
padded map (_.reduceLeft(_ + _)) foreach println |
|||
</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |