view chapter3/fileRead2.scala @ 0:b316eec6fa7a draft default tip

add sample files
author Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
date Tue, 08 Jan 2013 16:41:46 +0900
parents
children
line wrap: on
line source

import scala.io.Source
def widthOfLength(s: String) = s.length.toString.length
if (args.length > 0) {
    val lines = Source.fromFile(args(0)).getLines.toList
    val longestLine = lines.reduceLeft(
	(a, b) => if (a.length > b.length) a else b
    )
    val maxWidth = widthOfLength(longestLine)
    for (line <- lines) {
	val numSpaces = maxWidth - widthOfLength(line)
	val padding = " " * numSpaces
	println(padding + line.length +" | " + line)
    }
}
else
    Console.err.println("Please enter filename")