Reading text line

“Dan” falseflyboy@yahoo.comNONO wrote in message

i want to read a line of string from a file every other line

line one
line two
line three
line four

want it to print

line one
line three

Simple solution (without error handling):

f = File.open(“testfile”,“r”)
while(!f.eof)
s = f.gets
puts s
f.gets
end
f.close

“Shashank Date” sdate@everestkc.net wrote in message

f = File.open(“testfile”,“r”)
while(!f.eof)
s = f.gets
puts s
f.gets
end
f.close

Two one-liners (again ignoring exception handling, of course):

File.open(“junk.txt”,“r”) {|f| (puts f.gets; f.gets) while !f.eof}

File.readlines(“junk.txt”).each_with_index{|line,i| puts line if (i % 2 ==
0)}

file = FILE
ok = true
res = IO.readlines(file).reject { |obj| ok = !ok }
p res

···

On Sat, 21 Jun 2003 11:39:20 +0000, Shashank Date wrote:

“Shashank Date” sdate@everestkc.net wrote in message

f = File.open(“testfile”,“r”)
while(!f.eof)
s = f.gets
puts s
f.gets
end
f.close

Two one-liners (again ignoring exception handling, of course):

File.open(“junk.txt”,“r”) {|f| (puts f.gets; f.gets) while !f.eof}

File.readlines(“junk.txt”).each_with_index{|line,i| puts line if (i % 2 ==
0)}


Simon Strandgaard

Even better… you can use ‘partition’ instead

file = FILE
ok = true
res1, res2 = IO.readlines(file).partition { |obj| ok = !ok }
p res1, res2

···

On Sat, 21 Jun 2003 21:21:24 +0200, Simon Strandgaard wrote:

On Sat, 21 Jun 2003 11:39:20 +0000, Shashank Date wrote:

“Shashank Date” sdate@everestkc.net wrote in message

f = File.open(“testfile”,“r”)
while(!f.eof)
s = f.gets
puts s
f.gets
end
f.close

Two one-liners (again ignoring exception handling, of course):

File.open(“junk.txt”,“r”) {|f| (puts f.gets; f.gets) while !f.eof}

File.readlines(“junk.txt”).each_with_index{|line,i| puts line if (i % 2 ==
0)}

file = FILE
ok = true
res = IO.readlines(file).reject { |obj| ok = !ok }
p res


Simon Strandgaard

“Simon Strandgaard” 0bz63fz3m1qt3001@sneakemail.com

Even better… you can use ‘partition’ instead

file = FILE
ok = true
res1, res2 = IO.readlines(file).partition { |obj| ok = !ok }
p res1, res2

Hmm… did not know about partition. Thanks for pointing it out.

Since Ruby is a language with class :wink: I came up with this more
"classic" (generic) solution:

···

#----------------------------------------------------------------
class Array
def skip(n=0,from=0)
raise “Cannot skip negative elements” if n < 0
0.step(self.length-1,n+1){|i| yield(self[i+from]) if self[i+from]}
end
end

IO.readlines(“junk.txt”).skip(1){|line| puts line}
#----------------------------------------------------------------

A better, more generic solution would not use a[b] to index array elements.
If you limit yourself to using only ‘each’ then it would be compatible with
all classes which mix in Enumerable. Something like this:

module Enumerable
def each_skip(n=0, from=0)
skip = from
each do |item|
if skip > 0
skip -= 1
next
end
yield item
skip = n
end
end
end

Now, you can use this directly on the IO object - you don’t even have to
read the lines into an array first (although it works on arrays as well). If
you have a file with a million lines then that makes quite a difference :slight_smile:

File.open(“junk.txt”).each_skip(1) {|line| puts line}

Regards,

Brian.

···

On Mon, Jun 23, 2003 at 12:49:22AM +0900, Shashank Date wrote:

Since Ruby is a language with class :wink: I came up with this more
"classic" (generic) solution:

#----------------------------------------------------------------
class Array
def skip(n=0,from=0)
raise “Cannot skip negative elements” if n < 0
0.step(self.length-1,n+1){|i| yield(self[i+from]) if self[i+from]}
end
end

IO.readlines(“junk.txt”).skip(1){|line| puts line}
#----------------------------------------------------------------

Splendid !

– shanko

“Brian Candler” B.Candler@pobox.com wrote in message

A better, more generic solution would not use a[b] to index array
elements.
If you limit yourself to using only ‘each’ then it would be compatible
with
all classes which mix in Enumerable. Something like this:

module Enumerable
def each_skip(n=0, from=0)
skip = from
each do |item|
if skip > 0
skip -= 1
next
end
yield item
skip = n
end
end
end

Now, you can use this directly on the IO object - you don’t even have to
read the lines into an array first (although it works on arrays as well).
If
you have a file with a million lines then that makes quite a difference
:slight_smile:

···

File.open(“junk.txt”).each_skip(1) {|line| puts line}