# CSV nesting

**URL:** <https://www.ruby-forum.com/t/csv-nesting/135366>\
**Category:** Ruby\
**Created:** [April 21, 2008, 4:31pm UTC](https://www.ruby-forum.com/t/csv-nesting/135366 "2008-04-21T16:31:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shinji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c6cbf5/32.png) [@shinji](https://www.ruby-forum.com/u/shinji)\
**Post date:** [April 21, 2008, 4:31pm UTC](https://www.ruby-forum.com/t/csv-nesting/135366/1 "2008-04-21T16:31:59Z")

</div>

I have a snippet of code that looks like this:

```
next if row.nil?
for i in 0...row.length
  next if row[i].nil?
  f.puts " <#{fields[i]}>#{row[i]}</#{fields[i]}>"
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:

 Brown Bobby

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.

---

<div class="post-metadata">

**Author:** ![shinji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c6cbf5/32.png) [@shinji](https://www.ruby-forum.com/u/shinji)\
**Post date:** [April 21, 2008, 4:42pm UTC](https://www.ruby-forum.com/t/csv-nesting/135366/2 "2008-04-21T16:42:52Z")

</div>

I based the code orignally around this:

[http://snippets.dzone.com/posts/show/3701](http://snippets.dzone.com/posts/show/3701)

---

<div class="post-metadata">

**Author:** ![shinji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c6cbf5/32.png) [@shinji](https://www.ruby-forum.com/u/shinji)\
**Post date:** [April 21, 2008, 5:04pm UTC](https://www.ruby-forum.com/t/csv-nesting/135366/3 "2008-04-21T17:04:12Z")

</div>

On Mon, Apr 21, 2008 at 11:32:01PM +0900, Max R. wrote:

> 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.

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

enjoy,

-jeremy

---

<div class="post-metadata">

**Author:** ![shinji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c6cbf5/32.png) [@shinji](https://www.ruby-forum.com/u/shinji)\
**Post date:** [April 23, 2008, 12:59pm UTC](https://www.ruby-forum.com/t/csv-nesting/135366/4 "2008-04-23T12:59:59Z")

</div>

Max R. wrote:

> Jeremy H. wrote:
> 
> > Completely off the cuff, untested, but this is the approach I’m thinking  
> > of.
> > 
> > [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  
next if rows[i].nil?  
if not rows[surname] or rows[forename]  
puts rows[i]#“Current attribute: #{rows[i]}”  
end  
if rows[surname]  
puts “\<ORGANISATIONAL\_PERSON\>”  
#puts “row #{i}”  
puts " "  
puts " #{rows[i][surname]}"  
puts " "  
end  
if rows[forename]  
#puts “row #{i}”  
puts " "  
puts " #{rows[i][forename]}"  
puts " "  
end  
end

The problem being that an if, elsif else construction doesn’t seem to  
work…

---

<div class="post-metadata">

**Author:** ![shinji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c6cbf5/32.png) [@shinji](https://www.ruby-forum.com/u/shinji)\
**Post date:** [April 22, 2008, 3:05pm UTC](https://www.ruby-forum.com/t/csv-nesting/135366/5 "2008-04-22T15:05:16Z")

</div>

Jeremy H. wrote:

> Completely off the cuff, untested, but this is the approach I’m thinking  
> of.
> 
> [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.
