From: Shannon Fang Date: 2002-12-12T09:08:58+09:00 Subject: Re: help -- persuade my boss to adopt ruby Hi Leif, Thanks for the code. While reading your code, I remembered one thing I'm not happy with Java. Although it is much better than C++, I think Java is still quite low level language, it is artificially made complex by a huge tree of classes. When I learned Java, and checked the document I don't know which class will suit my need, for example, the PrintWriter class, I'm sure there are similar parent/child/sibling of it that does similar job... Ruby's class hierachy is simpler and it is easier to find suitable class/method for your purpose. What do you feel? Shannon On Thu, 12 Dec 2002 09:02:30 +0900 Leif Jantzen wrote: > Shannon Fang wrote: > > > Hi Ruby Lovers, > > > > File.open("output.txt","w+") do |f| > > File.open("input.txt").each_line do |line| > > fld=line.match(/.{4}(.{3})(.{6})(.{3}).{17}(.{6})/) > > f.print fld[0],"-",fld[1],"-",fld[2],fld[3] > > end > > end > > > > You might try something like the following. It has been tested on a sample > file of my own design... There may be better/more compact/idiomatic ways > to do this... > > Although this example is slightly more complex than the ruby example, this > should not be the only reason to choose ruby over java. Think carefully > before you choose. > > # File: ProcessFile.java > > import java.io.* ; > import java.util.regex.* ; > > class ProcessFile { > public static void main(String[] argv){ > > try { > File f_in = new File("input.txt"); > File f_out = new File("output.txt"); > String line ; > Pattern p = Pattern.compile("(.{4}(.{3})(.{6})(.{3}).{17}(.{6}))") ; > > LineNumberReader in = new LineNumberReader(new FileReader(f_in)); > PrintWriter out = new PrintWriter(new FileWriter(f_out)); > > while ( null != ( line=in.readLine())) { > Matcher m = p.matcher(line); > if ( true == m.find()) { > System.out.println(m.group(1) + "-" + m.group(2) + "-" + > m.group(3)+ m.group(4)); > } > } > } catch ( Exception e ) { > e.printStackTrace(); > } > } > }