From: James Edward Gray II Date: 2007-11-12T22:56:30+09:00 Subject: Re: comp.lang.fortran challenge On Nov 11, 2007, at 7:30 PM, Bil Kleb wrote: > Having Ruby fun with the comp.lang.fortran folks: > > http://tinyurl.com/38o8ex > http://tinyurl.com/2pw22q > > Please riff a better Ruby answer... That little challenge made me wish for some Ruby 1.9 elements, like an ordered Hash. James Edward Gray II #!/usr/bin/env ruby -wKU us, vs = Array.new, Array.new DATA.each do |line| if line =~ /^time\s*:\s*(.+?)\s*$/ [us, vs].each { |array| array.clear } puts "for time #{$1}" puts elsif line =~ /^([UV])(\s*\([^)]+\))\s*=\s*([\d.]+)\s*$/ ($1 == "U" ? us : vs) << [$2, $3.to_f] puts line end if line =~ /^\s*$/ or DATA.eof? next if us.empty? or vs.empty? us.each do |sig, value| next unless v = vs.assoc(sig) puts "UV%s = %.2f" % [sig, value * v.last] end puts end end __END__ ==================== time: 00 minutes ==================== velocity U in m/s U ( 1, 1, 1) = 12.34 U ( 1, 1, 2) = 10.00 U ( 1, 1, 3) = 11.01 U ( 1, 2, 1) = 10.05 U ( 1, 2, 2) = 12.40 U ( 1, 2, 3) = 11.20 U ( 1, 3, 1) = 12.80 U ( 1, 3, 2) = 10.30 U ( 1, 3, 3) = 11.25 velocity V in m/s V ( 1, 1, 1) = 11.40 V ( 1, 1, 2) = 12.00 V ( 1, 1, 3) = 13.50 V ( 1, 2, 1) = 11.00 V ( 1, 2, 2) = 11.70 V ( 1, 2, 3) = 11.25 V ( 1, 3, 1) = 11.50 V ( 1, 3, 2) = 10.60 V ( 1, 3, 3) = 11.23 ==================== time : 30 minutes ==================== velocity U in m/s U ( 1, 1, 1) = 13.30 U ( 1, 1, 2) = 11.00 U ( 1, 1, 3) = 11.30 U ( 1, 2, 1) = 10.00 U ( 1, 2, 2) = 12.30 U ( 1, 2, 3) = 10.10 U ( 1, 3, 1) = 10.90 U ( 1, 3, 2) = 11.40 U ( 1, 3, 3) = 11.75 velocity V in m/s V ( 1, 1, 1) = 12.40 V ( 1, 1, 2) = 11.00 V ( 1, 1, 3) = 11.60 V ( 1, 2, 1) = 11.20 V ( 1, 2, 2) = 11.90 V ( 1, 2, 3) = 11.35 V ( 1, 3, 1) = 12.50 V ( 1, 3, 2) = 11.60 V ( 1, 3, 3) = 13.20