How do i split a file by newline

Hi all,
I would like to know how do i split a file by newline ,am
able to do it tab space like

path=“E:/expr/amit.txt”
File.open(path).each do |i|
k=i.split("\t" )

puts “#{k[0]}”
end

o/p: amit

and this is content of my file(amit.txt)

amit sumit

now i changed content of file like

amit
sumit

path=“E:/expr/amit.txt”
File.open(path).each do |i|
k=i.split("\n" )

puts “#{k[0]}”
end

i should get amit only but am getting
amit
sumit

how do i make them split from newline??

Amit T. [email protected] wrote:

how do i make them split from newline??

no need to split("\n") because it’s allready done :
#! /opt/local/bin/ruby1.9

encoding: utf-8

k=[]
File.open(“amit.txt”).each {|l| k<<l }
puts k[0]

here i get amit, the first line.

=?ISO-8859-1?Q?Une_B=E9vue?= wrote in post #950421:

Amit T. [email protected] wrote:

how do i make them split from newline??

no need to split("\n") because it’s allready done :
#! /opt/local/bin/ruby1.9

encoding: utf-8

k=[]
File.open(“amit.txt”).each {|l| k<<l }
puts k[0]

here i get amit, the first line.

Thanks frnd
but why split("\n") is not working for me

Amit T. [email protected] wrote:

but why split("\n") is not working for me

i think because the :
File.open(“amit.txt”).each {|l| k<<l }
allready split the content of the file by line, then, no more \n ?
the “.each” here would means each line…

2010/10/15 Une B. [email protected]:

amit
sumit

how do i make them split from newline??

no need to split("\n") because it’s allready done :
#! /opt/local/bin/ruby1.9

encoding: utf-8

k=[]
File.open(“amit.txt”).each {|l| k<<l }
puts k[0]

Even simpler

k = File.readlines “amit.txt”

different separator

k = File.readlines “amit.txt”, “\t”

Cheers

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs