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
Phlip5
(Phlip)
29 November 2002 11:16
2
“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? –
ts1
(ts)
29 November 2002 11:24
3
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
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
Phlip5
(Phlip)
29 November 2002 19:57
5
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
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 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!
···
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
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
ts1
(ts)
30 November 2002 11:21
7
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
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