# Creating Arrays

**URL:** <https://rubytalk.org/t/creating-arrays/40687>\
**Category:** ruby-talk\
**Created:** [17 September 2007 16:44 UTC](https://rubytalk.org/t/creating-arrays/40687 "2007-09-17T16:44:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Nott](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@John\_Nott](https://rubytalk.org/u/John_Nott)\
**Post date:** [17 September 2007 16:44 UTC](https://rubytalk.org/t/creating-arrays/40687/1 "2007-09-17T16:44:00Z")

</div>

Dear All,\<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /\>

Please forgive me if this email is a bit simple, but I am new to ruby and object orientated programming pretty much and just looking for a few tips from the experts.

I have a .csz file created from a spreadsheet which contains 666 rows and 19 columns. What I would like to do is read the csz file into an array in ruby and store that array as a set of columns and rows (like in excel). Once I have achieved this, I will be making some updates to all the figures within the Array.

At the moment my code looks like this:

print "Importing Planning Sheet: 2004\_extrap\_Planning\_Data\n"

filepath = 'Q:\\TECH2006\\DBA\\02\_LU\\Final TAGM 2007 (working)\\TAGM\_666\_Current\_FROM G\_DRIVE\\2004\\2004\_extrap\_Planning\_Data.csv'

Input\_file = File.new(filepath, 'r')

puts "READING THE FILE"

fields = Array.new

while ( !Input\_file.eof )

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line = Input\_file.gets

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line.each(',') { |this\_record|

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fields \<\< this\_record.chomp(",").strip()

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

Input\_file.each { |line| }

End

What I understand is happening is that I am reading my file into a new Ruby file called "Input\_file" and then I am creating a new Array called "fields" and I am loading the new file into the fields array but removing the commas using the chomp in each line. Unfortunately it isn't working!

My question is basically how do I do what I want to do?... It is difficult for a new starter but I suspect you guys would know it inside out!

Any tips would be greatly appreciated!

Cheers!

- John

> **···**
>
> #####################################################################################  
> This e-mail message has been scanned for Viruses and Content and cleared  
> by MailMarshal  
> #####################################################################################

---

<div class="post-metadata">

**Author:** ![Ronald\_Fischer1](https://avatars.discourse-cdn.com/v4/letter/r/c2a13f/32.png) [@Ronald\_Fischer1](https://rubytalk.org/u/Ronald_Fischer1)\
**Post date:** [18 September 2007 07:39 UTC](https://rubytalk.org/t/creating-arrays/40687/2 "2007-09-18T07:39:21Z")

</div>

> I have a .csz file created from a spreadsheet which contains  
> 666 rows and 19 columns. What I would like to do is read the  
> csz file into an array in ruby and store that array as a set  
> of columns and rows (like in excel).

[snip]

> My question is basically how do I do what I want to do?... It  
> is difficult for a new starter but I suspect you guys would  
> know it inside out!

Since you want to mimic the spreadsheet idiom (a table of rows  
and columns), I suggest you create an "array of arrays".  
Something like (code not tested!):

table= # create new array  
open("your filename goes here").each do |line|  
&nbsp;&nbsp;fields= # array of fields in each line  
&nbsp;&nbsp;line.each(',') { |field|  
&nbsp;&nbsp;&nbsp;&nbsp;fields \<\< field.chomp(',').strip  
&nbsp;&nbsp;}  
&nbsp;&nbsp;table \<\< fields  
end

> **···**
>
> --  
> Ronald Fischer \<ronald.fischer@venyon.com\>  
> Phone: +49-89-452133-162

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [18 September 2007 09:18 UTC](https://rubytalk.org/t/creating-arrays/40687/3 "2007-09-18T09:18:05Z")

</div>

Plus the reading / parsing could be done by a CSV library.

Kind regards

robert

> **···**
>
> 2007/9/18, Ronald Fischer \<ronald.fischer@venyon.com\>:
> 
> > \> I have a .csz file created from a spreadsheet which contains  
> > \> 666 rows and 19 columns. What I would like to do is read the  
> > \> csz file into an array in ruby and store that array as a set  
> > \> of columns and rows (like in excel).
> > 
> > [snip]
> > 
> > \> My question is basically how do I do what I want to do?... It  
> > \> is difficult for a new starter but I suspect you guys would  
> > \> know it inside out!
> > 
> > Since you want to mimic the spreadsheet idiom (a table of rows  
> > and columns), I suggest you create an "array of arrays".  
> > Something like (code not tested!):
> > 
> > table= # create new array  
> > open("your filename goes here").each do |line|  
> > &nbsp;&nbsp;fields= # array of fields in each line  
> > &nbsp;&nbsp;line.each(',') { |field|  
> > &nbsp;&nbsp;&nbsp;&nbsp;fields \<\< field.chomp(',').strip  
> > &nbsp;&nbsp;}  
> > &nbsp;&nbsp;table \<\< fields  
> > end

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [18 September 2007 09:35 UTC](https://rubytalk.org/t/creating-arrays/40687/4 "2007-09-18T09:35:06Z")

</div>

[...]

class String  
&nbsp;&nbsp;def csv  
&nbsp;&nbsp;&nbsp;&nbsp;if include? '"'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ary =  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"#{chomp},".scan(  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/\G"([^"]\*(?:""[^"]\*)\*)",|\G([^,"]\*),/ )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise "Bad csv record:\n#{self}" if $' != ""  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ary.map{|a| a[1] || a[0].gsub(/""/,'"') }  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ary = chomp.split( /,/, -1)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;## "".csv ought to be [""], not , just as  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;## ",".csv is ["",""].  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if == ary  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[""]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ary  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

array = nil  
File.open('junk'){|f|  
&nbsp;&nbsp;array = f.readlines.map{|s| s.csv }  
}  
p array

> **···**
>
> On Sep 18, 2:39 am, "Ronald Fischer" \<ronald.fisc...@venyon.com\> wrote:
> 
> > \> I have a .csz file created from a spreadsheet which contains  
> > \> 666 rows and 19 columns. What I would like to do is read the  
> > \> csz file into an array in ruby and store that array as a set  
> > \> of columns and rows (like in excel).
> > 
> > [snip]
> > 
> > \> My question is basically how do I do what I want to do?... It  
> > \> is difficult for a new starter but I suspect you guys would  
> > \> know it inside out!
> > 
> > Since you want to mimic the spreadsheet idiom (a table of rows  
> > and columns), I suggest you create an "array of arrays".
