Loop starting not from beginning

Hi Ruby friends,

I have lots of file loops w the ff form:

File.open(“test”) do |f|
f.each do |line|
p line
end
end

However, the file has headers that I want to skip. So I skip first few line
of headers like …

scheme 1:

File.open(“test”) do |f|
f.each do |line|
if f.lineno <= 5 then next end
p line
end
end

or

scheme 2:

File.open(“test”) do |f|
f.readline <---- just read
f.readline
f.each do |line|
p line
end
end

and I combine scheme 1 & 2 like:

File.open(“test”) do |f|
while (f.lineno <= 4)
f.readline
end
f.each do |line|
p line
end
end

Is there any other way better than this?
I like something that when it opens, its already on the line that I want…
or maybe, I’m asking too much… I’m just newbie, so pls bear w me…

Many thanks,
-botp

“Peña, Botp” wrote:

File.open(“test”) do |f|
while (f.lineno <= 4)
f.readline
end
f.each do |line|
p line
end
end

File.open(“test”) do |f|
f.readlines.slice(5…-1) do |line|
p line
end
end

New question. What, if anything, is wrong with this?

File.open(“test”).readlines.slice(5…-1) do |line|
p line
end

I ask because I’ve experienced trouble treating the return value of open as
a file object, without the magic block mode.

···


Phlip
greencheese.org
– Who needs a degree when we have Web e-search engines? –

New question. What, if anything, is wrong with this?
File.open("test").readlines.slice(5..-1) do |line|

#slice don't take a block as argument :slight_smile:

Guy Decoux

Hi,

New question. What, if anything, is wrong with this?

File.open(“test”).readlines.slice(5…-1) do |line|
p line
end

I ask because I’ve experienced trouble treating the return value of open as
a file object, without the magic block mode.

I’m not sure what trouble you’ve experienced, perhaps the file
wasn’t closed.

IO.readlines(“test”).slice(5…-1)

will work.

And regarding original post,

open(“test”) do |f|
4.times {f.readline}
f.each {|line| p line}
end

···

At Fri, 29 Nov 2002 20:16:30 +0900, Phlip wrote:


Nobu Nakada

ts wrote:

New question. What, if anything, is wrong with this?
File.open(“test”).readlines.slice(5…-1) do |line|

#slice don’t take a block as argument :slight_smile:

Apologies to the newbie, but I think … each … of the rest of us can
figure out how to fix that.

···


Phlip
greencheese.org
– Have a :slight_smile: day –

In
File.open(“test”).readlines.slice(5…-1).each do |line|

end

File.open is working as a sinonym of File.new, so the File will remain
open, for the block is consumed by #each, not by #open.

Now that last sentence sounds horrible! :stuck_out_tongue:

···

On Sat, Nov 30, 2002 at 04:57:32AM +0900, Phlip wrote:

ts wrote:

New question. What, if anything, is wrong with this?
File.open(“test”).readlines.slice(5…-1) do |line|

#slice don’t take a block as argument :slight_smile:

Apologies to the newbie, but I think … each … of the rest of us can
figure out how to fix that.


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

After 14 non-maintainer releases, I’m the S-Lang non-maintainer.
– Ray Dassen

ts wrote:

New question. What, if anything, is wrong with this?

                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File.open("test").readlines.slice(5..-1) do |line|

#slice don't take a block as argument :slight_smile:

Apologies to the newbie, but I think ... each ... of the rest of us can
figure out how to fix that.

You have just asked what is wrong with something that you've not taken the
times to test.

Now if you don't see that #readlines create an array, and #slices create
another just to iterate with #each, this is another problem.

Guy Decoux