Read text file

Hi,
   I have one text file for example

9115 2008-06-20 07:26:35
91415 2008-06-20 07:26:32
9315 2008-06-20 07:26:33
2115 2008-06-20 07:26:05
15 2008-06-20 07:26:34
115 2008-06-20 07:26:35

i need to read only first column i.e numbers like 9115 etc if you have
any solution reply me

···

--
Posted via http://www.ruby-forum.com/.

Pragash Mr. wrote:

Hi,
   I have one text file for example

9115 2008-06-20 07:26:35
91415 2008-06-20 07:26:32
9315 2008-06-20 07:26:33
2115 2008-06-20 07:26:05
15 2008-06-20 07:26:34
115 2008-06-20 07:26:35

i need to read only first column i.e numbers like 9115 etc if you have
any solution reply me

Try this:

file = File.open("name.txt")
columns =
file.each_line do |line|
columns << line.split(" ")[0]
end
p columns

···

--
Posted via http://www.ruby-forum.com/\.

Pragash Mr. wrote:

15 2008-06-20 07:26:34
115 2008-06-20 07:26:35

i need to read only first column i.e numbers like 9115 etc if you have
any solution reply me

numbers = File.open(filename) do |f|
  f.map do |line|
    line.to_i
  end
end

HTH,
Sebastian

···

--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Hi, there is another solution:

File.open('pp.txt') do |infile|
  while line = infile.gets #the loop ends if infile.gets == EOF
    line_split = line.split(" ") #split line through spaces; return
vector
    puts line_split[0] #the first element of the line is the
number
  end
end

for more information of this topic:

http://pleac.sourceforge.net/pleac_ruby/fileaccess.html

···

--
Posted via http://www.ruby-forum.com/.