Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ruby's Enumerable contains <a href="http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-slice_before" rel="nofollow"><code>slice_before</code></a> which is great for this sort of task, breaking down a file into blocks based on some marker.</p> <pre><code>require 'pp' blocks = DATA.readlines.map(&amp;:strip).reject{ |l| l == '' }.slice_before(/\A\*{3}/) pp blocks.to_a __END__ *** Summary *** Job Name = test Date created: Mon Jan 14 15:48:33 2013 *** Analysis Information Steady State is ON Turbulent Incompressible Flow is ON Static Temperature Equation is ON Mixed Convection is ON *** Field Variable Results Summary For Iteration 300 Var Mean at Max at Min Vx Vel +5.71519e+002 1320103 +3.02718e+004 1319857 -2.66582e+004 mm/s Vy Vel +3.40035e+002 158922 +2.79257e+004 1319731 -1.42855e+004 mm/s Vz Vel -7.17959e+002 1318038 +1.62986e+004 1319053 -2.21582e+004 mm/s Press -2.05980e+001 50858 +5.19412e+003 50905 -1.44865e+003 N/m^2 Temp +4.60000e+001 10965 +4.60000e+001 315867 +4.60000e+001 C TurbK +1.19616e+006 1319490 +1.44421e+008 10966 +1.81700e-008 mm^2/s^2 TurbD +1.71412e+009 1319490 +2.88554e+011 233065 +5.37798e-004 mm^2/s^3 Scal1 +0.00000e+000 10965 +0.00000e+000 315867 +0.00000e+000 PTotl -5.91285e+000 50858 +5.19412e+003 50905 -1.44865e+003 N/m^2 EVisc +2.52037e-004 1320370 +1.14488e-002 2229 +0.00000e+000 g/mm-s ECond +1.05355e-002 1352833 +5.88890e-002 2229 +0.00000e+000 W/mm-K Dens +2.34793e-004 58024 +3.43080e-003 315867 +1.20473e-006 g/mm^3 Visc +1.62605e-005 10965 +1.81700e-005 2229 +0.00000e+000 g/mm-s Cond +2.50840e-002 2229 +2.04000e-001 315867 +2.56300e-005 W/mm-K SpecH +1.01202e+000 38432 +1.81000e+000 10249 +1.00500e-003 J/g-K Emiss +8.94911e-001 10965 +1.00000e+000 2229 +0.00000e+000 Transmiss +0.00000e+000 0 +0.00000e+000 0 +0.00000e+000 WRough +0.00000e+000 10965 +0.00000e+000 315867 +0.00000e+000 mm SeeBeck +0.00000e+000 0 +0.00000e+000 0 +0.00000e+000 V/K GenT +1.11977e+003 223286 +1.18027e+005 584515 +3.19558e-013 1/s </code></pre> <p>I shortened the data because there was too much to use for a sample.</p> <p>Running the code outputs:</p> <pre> [["*** Summary ***", "Job Name = test Date created: Mon Jan 14 15:48:33 2013"], ["*** Analysis Information", "Steady State is ON", "Turbulent Incompressible Flow is ON", "Static Temperature Equation is ON", "Mixed Convection is ON"], ["*** Field Variable Results Summary For Iteration 300", "Var Mean at Max at Min", "Vx Vel +5.71519e+002 1320103 +3.02718e+004 1319857 -2.66582e+004 mm/s", "Vy Vel +3.40035e+002 158922 +2.79257e+004 1319731 -1.42855e+004 mm/s", "Vz Vel -7.17959e+002 1318038 +1.62986e+004 1319053 -2.21582e+004 mm/s", "Press -2.05980e+001 50858 +5.19412e+003 50905 -1.44865e+003 N/m^2", "Temp +4.60000e+001 10965 +4.60000e+001 315867 +4.60000e+001 C", "TurbK +1.19616e+006 1319490 +1.44421e+008 10966 +1.81700e-008 mm^2/s^2", "TurbD +1.71412e+009 1319490 +2.88554e+011 233065 +5.37798e-004 mm^2/s^3", "Scal1 +0.00000e+000 10965 +0.00000e+000 315867 +0.00000e+000", "PTotl -5.91285e+000 50858 +5.19412e+003 50905 -1.44865e+003 N/m^2", "EVisc +2.52037e-004 1320370 +1.14488e-002 2229 +0.00000e+000 g/mm-s", "ECond +1.05355e-002 1352833 +5.88890e-002 2229 +0.00000e+000 W/mm-K", "Dens +2.34793e-004 58024 +3.43080e-003 315867 +1.20473e-006 g/mm^3", "Visc +1.62605e-005 10965 +1.81700e-005 2229 +0.00000e+000 g/mm-s", "Cond +2.50840e-002 2229 +2.04000e-001 315867 +2.56300e-005 W/mm-K", "SpecH +1.01202e+000 38432 +1.81000e+000 10249 +1.00500e-003 J/g-K", "Emiss +8.94911e-001 10965 +1.00000e+000 2229 +0.00000e+000", "Transmiss +0.00000e+000 0 +0.00000e+000 0 +0.00000e+000", "WRough +0.00000e+000 10965 +0.00000e+000 315867 +0.00000e+000 mm", "SeeBeck +0.00000e+000 0 +0.00000e+000 0 +0.00000e+000 V/K", "GenT +1.11977e+003 223286 +1.18027e+005 584515 +3.19558e-013 1/s"]] </pre> <p>The file was converted to an array of arrays. Leading and trailing whitespace, along with new-lines and carriage-returns were stripped, and blank lines were removed.</p> <p>Processing the file further is done using a loop over the outer array, and a test that looks at the first line of each sub-array to determine what to do with that block. Something like this would be a starting point:</p> <pre><code>hash = {} blocks.each do |block| case block.shift when /Summary/ # process the summary information when /Analysis Information/ # process the analysis information hash[:analysis_information] = Hash[block.map{ |r| r.split(/ +is +/) }] when /Field Variable Results/ # process the field variable results end end </code></pre> <p>The idea is that when the code is done, <code>hash</code> will contain parsed data in a hash of hashes or hash of arrays, ready for you to write it out. I'd recommend looking at using YAML, because it makes short work of serializing the data to a file.</p> <p>I'm not going to add more, because the question sounds very much like a homework assignment, plus parsing the lines isn't that hard. Breaking the file into blocks is a bigger task, and that part is done for you already.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload