Read CSV-File (Comma Separated Values)

Hi!

First of all, im a real beginner in ruby.

How can I read data from a file? How to separate it, is my second
question.
So, i don't want to become the File from the STDIN, but to set the uri
from the file with uri = '/home/hu8/Documents/data.csv' or somthing
like this...

btw: How can I read user input, so i make:
print "Type A or B"

and then the user must type a or b and i become in a variable (like
input = user.input()?)?
Or does exists a function like the PHP-function fgetcsv()?

* hu8 <jakobfastenbauer196@hotmail.com> [2005-01-30]:
  ^^^
You should post with a real name here.

How can I read data from a file? How to separate it, is my
second question.

begin
  file = File.open('filename')
  while not file.eof?
    values = file.gets.split(/,/)
    # do some stuff with it
  end
ensure
  file.close
end

With this code, you have an array "values" within the
while-loop. "values" contains all the fields which were
seperated by comma in the file.

Julius

For CSV files take a look at the CSV module in the stdlib:
http://www.ruby-doc.org/stdlib/libdoc/csv/rdoc/index.html

For opening arbitrary URIs take a look at open-uri in the std-lib:
http://www.ruby-doc.org/stdlib/libdoc/open-uri/rdoc/index.html
HTH,
Assaph

Julius Plenz wrote:

* hu8 <jakobfastenbauer196@hotmail.com> [2005-01-30]:
  ^^^
You should post with a real name here.

How can I read data from a file? How to separate it, is my
second question.

begin
  file = File.open('filename')
  while not file.eof?
    values = file.gets.split(/,/)
    # do some stuff with it
  end
ensure
  file.close
end

You might do better to leave the hard work to ruby:

IO.readlines( 'filename' ).each{ |line|
    ary_values = line.split(/,/)
    # do some stuff with it
}

By using the block form you let Ruby handle the check for EOF and close the file when done.

James

James Britt wrote:

You might do better to leave the hard work to ruby:

IO.readlines( 'filename' ).each{ |line|
ary_values = line.split(,)

                         ^^^
Shouldn't there be quotes around the comma?
               line.split(',')

···

# do some stuff with it
}

By using the block form you let Ruby handle the check for EOF and close
the file when done.

Stefan Lang wrote:

James Britt wrote:

You might do better to leave the hard work to ruby:

IO.readlines( 'filename' ).each{ |line|
ary_values = line.split(,)

                         ^^^
Shouldn't there be quotes around the comma?
               line.split(',')

Or regexp slashes, which is what I used, and which is what appears when I view my post in my E-mail reader (Thunderbird).

line.split(/,/)

Odd.

James

James Britt wrote:

Stefan Lang wrote:

James Britt wrote:

You might do better to leave the hard work to ruby:

IO.readlines( 'filename' ).each{ |line|
ary_values = line.split(,)

                         ^^^
Shouldn't there be quotes around the comma?
               line.split(',')

Or regexp slashes, which is what I used, and which is what appears when
I view my post in my E-mail reader (Thunderbird).

line.split(/,/)

Odd.

Sorry, seems to be a bug in KNode. The slashes are actually in
the message but my KNode doesn't show them!

···

James