# Creating Arrays

**URL:** <https://www.ruby-forum.com/t/creating-arrays/114805>\
**Category:** Ruby\
**Created:** [September 17, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/creating-arrays/114805 "2007-09-17T18:45:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jngo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e95f7d/32.png) [@jngo](https://www.ruby-forum.com/u/jngo)\
**Post date:** [September 17, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/creating-arrays/114805/1 "2007-09-17T18:45:57Z")

</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 )

```
        line = Input_file.gets

        line.each(',') { |this_record|

                    fields << this_record.chomp(",").strip()

                     }

```

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:** ![jngo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e95f7d/32.png) [@jngo](https://www.ruby-forum.com/u/jngo)\
**Post date:** [September 18, 2007, 9:40am UTC](https://www.ruby-forum.com/t/creating-arrays/114805/2 "2007-09-18T09:40:10Z")

</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|  
fields=[] # array of fields in each line  
line.each(’,’) { |field|  
fields \<\< field.chomp(’,’).strip  
}  
table \<\< fields  
end

---

<div class="post-metadata">

**Author:** ![jngo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e95f7d/32.png) [@jngo](https://www.ruby-forum.com/u/jngo)\
**Post date:** [September 18, 2007, 11:19am UTC](https://www.ruby-forum.com/t/creating-arrays/114805/3 "2007-09-18T11:19:27Z")

</div>

2007/9/18, Ronald F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> table \<\< fields  
> end

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

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![jngo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e95f7d/32.png) [@jngo](https://www.ruby-forum.com/u/jngo)\
**Post date:** [September 18, 2007, 11:35am UTC](https://www.ruby-forum.com/t/creating-arrays/114805/4 "2007-09-18T11:35:31Z")

</div>

On Sep 18, 2:39 am, “Ronald F.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Since you want to mimic the spreadsheet idiom (a table of rows  
> and columns), I suggest you create an “array of arrays”.  
> […]

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

array = nil  
File.open(‘junk’){|f|  
array = f.readlines.map{|s| s.csv }  
}  
p array
