# CSV nesting

**URL:** <https://rubytalk.org/t/csv-nesting/46009>\
**Category:** ruby-talk\
**Created:** [21 April 2008 14:32 UTC](https://rubytalk.org/t/csv-nesting/46009 "2008-04-21T14:32:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Max\_Russell](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@Max\_Russell](https://rubytalk.org/u/Max_Russell)\
**Post date:** [21 April 2008 14:32 UTC](https://rubytalk.org/t/csv-nesting/46009/1 "2008-04-21T14:32:01Z")

</div>

I have a snippet of code that looks like this:

&nbsp;&nbsp;&nbsp;&nbsp;next if row.nil?  
&nbsp;&nbsp;&nbsp;&nbsp;for i in 0...row.length  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next if row[i].nil?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.puts " \<#{fields[i]}\>#{row[i]}\</#{fields[i]}\>"  
&nbsp;&nbsp;&nbsp;&nbsp;end

this is part of a small script that parses a CSV document and then  
creates an XML output file.

However, I would like to add support for nesting elements in the output,  
so that if the current column/value is surname or forename I can write  
my output in a nested fashion:

\<PERSON\>  
&nbsp;&nbsp;\<SURNAME\>Brown\</SURNAME\>  
&nbsp;&nbsp;\<FORENAME\>Bobby\</FORENAME\>  
\</PERSON\>

The problem I'm having is with the identification of the current element  
as either SURNAME or FORENAME

I've tried using:

if #{fields[i]} == "\<ORGANISATION\_PERSON\_SURNAME\>"

(where fields is CSV.shift) and I've also tried

if row[i].data == 'ORGANISATION\_PERSON\_SURNAME'

What is the best way of acessing this please?

I've attached my original script for perusal.

Attachments:  
[http://www.ruby-forum.com/attachment/1765/csv\_xml.rb](http://www.ruby-forum.com/attachment/1765/csv_xml.rb)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jeremy\_Hinegardner](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@Jeremy\_Hinegardner](https://rubytalk.org/u/Jeremy_Hinegardner)\
**Post date:** [21 April 2008 15:03 UTC](https://rubytalk.org/t/csv-nesting/46009/2 "2008-04-21T15:03:17Z")

</div>

> I have a snippet of code that looks like this:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;next if row.nil?  
> &nbsp;&nbsp;&nbsp;&nbsp;for i in 0...row.length  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next if row[i].nil?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.puts " \<#{fields[i]}\>#{row[i]}\</#{fields[i]}\>"  
> &nbsp;&nbsp;&nbsp;&nbsp;end
> 
> this is part of a small script that parses a CSV document and then  
> creates an XML output file.
> 
> However, I would like to add support for nesting elements in the output,  
> so that if the current column/value is surname or forename I can write  
> my output in a nested fashion:
> 
> \<PERSON\>  
> &nbsp;&nbsp;\<SURNAME\>Brown\</SURNAME\>  
> &nbsp;&nbsp;\<FORENAME\>Bobby\</FORENAME\>  
> \</PERSON\>
> 
> The problem I'm having is with the identification of the current element  
> as either SURNAME or FORENAME
> 
> I've tried using:
> 
> if #{fields[i]} == "\<ORGANISATION\_PERSON\_SURNAME\>"
> 
> (where fields is CSV.shift) and I've also tried
> 
> if row[i].data == 'ORGANISATION\_PERSON\_SURNAME'
> 
> What is the best way of acessing this please?

I would use FasterCSV and loop over each row, and have some controlling  
information that shows which row fields should be grouped into a higher tag.

> I've attached my original script for perusal.
> 
> Attachments:  
> [http://www.ruby-forum.com/attachment/1765/csv\_xml.rb](http://www.ruby-forum.com/attachment/1765/csv_xml.rb)

Completely off the cuff, untested, but this is the approach I'm thinking of.

&nbsp;&nbsp;[http://p.ramaze.net/1154](http://p.ramaze.net/1154)

enjoy,

-jeremy

> **···**
>
> On Mon, Apr 21, 2008 at 11:32:01PM +0900, Max Russell wrote:
> 
> # --
> 
> Jeremy Hinegardner jeremy@hinegardner.org

---

<div class="post-metadata">

**Author:** ![Max\_Russell](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@Max\_Russell](https://rubytalk.org/u/Max_Russell)\
**Post date:** [22 April 2008 13:05 UTC](https://rubytalk.org/t/csv-nesting/46009/3 "2008-04-22T13:05:24Z")

</div>

Jeremy Hinegardner wrote:

> Completely off the cuff, untested, but this is the approach I'm thinking  
> of.
> 
> &nbsp;&nbsp;[http://p.ramaze.net/1154](http://p.ramaze.net/1154)
> 
> enjoy,
> 
> -jeremy

Thanks, although after looking at the FasterCSV docs. I'm thinking of  
working with an array or arrays instead of a Hash.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Max\_Russell](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@Max\_Russell](https://rubytalk.org/u/Max_Russell)\
**Post date:** [23 April 2008 11:00 UTC](https://rubytalk.org/t/csv-nesting/46009/4 "2008-04-23T11:00:05Z")

</div>

Max Russell wrote:

> Jeremy Hinegardner wrote:
> 
> > Completely off the cuff, untested, but this is the approach I'm thinking  
> > of.
> > 
> > &nbsp;&nbsp;[http://p.ramaze.net/1154](http://p.ramaze.net/1154)
> > 
> > enjoy,
> > 
> > -jeremy
> 
> Thanks, although after looking at the FasterCSV docs. I'm thinking of  
> working with an array or arrays instead of a Hash.

I've attached what I'm now using. I'm finding however that, due to the  
condition execution, my current code spits out all the other attributes  
before the two conditions:

for i in 1..rows.length  
&nbsp;&nbsp;next if rows[i].nil?  
&nbsp;&nbsp;if not rows[surname] or rows[forename]  
&nbsp;&nbsp;&nbsp;&nbsp;puts rows[i]#"Current attribute: #{rows[i]}"  
&nbsp;&nbsp;end  
&nbsp;&nbsp;if rows[surname]  
&nbsp;&nbsp;&nbsp;&nbsp;puts "\<ORGANISATIONAL\_PERSON\>"  
&nbsp;&nbsp;&nbsp;&nbsp;#puts "row #{i}"  
&nbsp;&nbsp;&nbsp;&nbsp;puts " \<SURNAME\>"  
&nbsp;&nbsp;&nbsp;&nbsp;puts " #{rows[i][surname]}"  
&nbsp;&nbsp;&nbsp;&nbsp;puts " \</SURNAME\>"  
&nbsp;&nbsp;end  
&nbsp;&nbsp;if rows[forename]  
&nbsp;&nbsp;&nbsp;&nbsp;#puts "row #{i}"  
&nbsp;&nbsp;&nbsp;&nbsp;puts " \<FORENAME\>"  
&nbsp;&nbsp;&nbsp;&nbsp;puts " #{rows[i][forename]}"  
&nbsp;&nbsp;&nbsp;&nbsp;puts " \</FORENAME\>"  
&nbsp;&nbsp;end  
end

The problem being that an if, elsif else construction doesn't seem to  
work...

Attachments:  
[http://www.ruby-forum.com/attachment/1769/fstcsv\_xml\_arrarrs.rb](http://www.ruby-forum.com/attachment/1769/fstcsv_xml_arrarrs.rb)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
