Note that there are some explanatory texts on larger screens.

plurals
  1. POCSV processing with1.9.2 - Multiple Rows being returned as a single array when using options hash
    primarykey
    data
    text
    <p>We have just started using Ruby 1.9.2 so we are now using CSV as opposed to FasterCSV (well, we are still using FasterCSV but you know what I mean).</p> <p>I cant get the code in example 2 below to ignore the header row using options. When I add ANY options to CSV.foreach it merges the entire csv file and all its rows into a single array as opposedd to an array per row. Working example below:</p> <p>The following code works fine for me (both cases i am using the same file:</p> <pre><code>CSV.foreach(csv_import.path) do |row_data| puts row_data.inspect end </code></pre> <p>The result is:</p> <pre><code>["a", "b", "c", "d", "e", "f", "g", "h"] ["1", "1", "1", "1", "1", "1", "1", "1"] ["2", "2", "2", "2", "2", "2", "2", "2"] </code></pre> <p>PERFECT! thats what I want except I want to ignore ["a", "b", "c", "d", "e", "f", "g", "h"]</p> <p>So now I write the code:</p> <pre><code>CSV.foreach(csv_import.path, :headers =&gt; true) do |row_data| puts row_data.inspect end </code></pre> <p>The result is 1 single row:</p> <p>["a", "b", "c", "d", "e", "f", "g", "h\n1", "1", "1", "1", "1", "1", "1", "1\n2", "2", "2", "2", "2", "2", "2", "2"]</p> <p>Im sure I have stuffed up something in the options Hash, but I dont know what it is? I have tried strings values, string keys, wrapping the options with {}, but to no avail. I have tried row_sep ="\n" also.</p> <p>I am a RoR newbie, can anyone shed some light on what error I am making here?</p> <hr> <p>Thanks for the response Ziggy, but i had no luck. It appears CSV.foreach is returning and Array for me? Would JRuby have an affect:</p> <p>This is my version</p> <p>jruby 1.6.0 (ruby 1.9.2 patchlevel 136) (2011-03-15 f3b6154) (Java HotSpot(TM) 6 4-Bit Server VM 1.6.0_24) [Windows 7-amd64-java]</p> <p>I tried your code and got the following:</p> <pre><code>CSV.foreach(csv_import.path) do |row_data| puts row_data.class puts row_data.length end </code></pre> <p>output:</p> <p>Array 8 Array 8 Array 8</p> <pre><code>CSV.foreach(csv_import.path, :headers =&gt; :first_row, :return_headers =&gt; false) do |row_data| puts row_data.class puts row_data.length end </code></pre> <p>output:</p> <p>Array 22</p> <p>I know its a simple workaround to ignore the headers programmatically, just wanted to know if I was using the library incorrectly.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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