I’ve got the file IO hammered out. I think I can read each line into a string
and then parse into an array using:
arr = IO.readlines(“printers.txt”)
columns_ar = Array.new()
columns_ar = arr[0].split("\t")
p columns_ar
p columns_ar.length
I know I can read each line and split it after I read it. But I’m not sure how
to populate my arrays? Should I use one array for each column? If so, how do I
create the correct number of arrays (and name them appropriately) at runtime?
Should I use 1 multidimensional array? Or an hash of some kind? The order of
these things is important.
Thanks very much for everyone’s help. This list is great. I want to try to
introduce Ruby at all my client sites.
Christopher
Christopher J. Meisenzahl CPS, CSTE
Senior Software Testing Consultant
Spherion
christopher.j.meisenzahl@citicorp.com
(585)-248-7749
···
-----Original Message-----
From: Meisenzahl, Christopher J.
Sent: Tuesday, January 28, 2003 7:49 AM
To: 'ruby-talk@ruby-lang.org’
Subject: Questions from a Ruby Newbie (file io and data structures)…
I’ve decided on a small project to attempt to learn Ruby beyond just flipping
through the book. By the way, I can’t say enough good things about the pickaxe
book. I picked it up in spite having the electronic version.
I would like to read in a tab (or comma) delimited text file one line or one
item at a time. The input file might look like this:
col1,col2,col3
a,b,c
d,e,f
g,h
j
Note that there is not the same amount of data in each column. It could vary.
What is the best way to read this in from a file? When I’m done I would like to
have all the items in col1 in an array, all the items in col2 in an array, and
so on.
I’m sure this is a rudimentary task, but I would like to see the most elegant
ways in which Ruby permits something like this to be done.
Any other thoughts very much appreciated!
Thanks very much in advance!
Christopher
Christopher J. Meisenzahl CPS, CSTE
Senior Software Testing Consultant
Spherion
christopher.j.meisenzahl@citicorp.com