How to jump over the first line in a file? (newbie)

M

Mark Toth

I have this code:

file = "filename.txt"
File.open(file) do |sFile|
while line = sFile.gets
row = line.split("\t")
a_product = Product.new(
:sku => row[0] ,
:name => row[1] ,
:price => row[2] ,
a_product.save
end
end

What if I want to jump over the first line of the file so it won´t be
imported?

Thank you,

Mark
 
S

Stefano Crocco

Alle venerd=C3=AC 28 dicembre 2007, Mark Toth ha scritto:
I have this code:

file =3D "filename.txt"
File.open(file) do |sFile|
while line =3D sFile.gets
row =3D line.split("\t")
a_product =3D Product.new(

:sku =3D> row[0] ,
:name =3D> row[1] ,
:price =3D> row[2] ,

a_product.save
end
end

What if I want to jump over the first line of the file so it won=C2=B4t be
imported?

Thank you,

Mark

A possibility is to do a call to sFile.gets before the while cycle. This wa=
y,=20
the first line of the file is read but not used.

Stefano
 
M

Mark Toth

Stefano said:
Alle venerdì 28 dicembre 2007, Mark Toth ha scritto:
:price => row[2] ,
Mark
A possibility is to do a call to sFile.gets before the while cycle. This
way,
the first line of the file is read but not used.

Stefano

Hmmm, it sounds like a great idea! Can you please send me some code for
it?

/Mark
 
M

Mark Toth

Thomas said:
Just add a sFile.gets before the while.

Okeeeey, so you mean like this?:

sFile.gets
file = "filename.txt"
File.open(file) do |sFile|
while line = sFile.gets
row = line.split("\t")
a_product = Product.new(
:sku => row[0] ,
:name => row[1] ,
:price => row[2] ,
a_product.save
end
end
 
L

Lee Jarvis

File#foreach anyone?

You might want to check it out, using gets before the while loop will do
the job too, though.

Regards,
Lee
 
L

lrlebron

I have this code:

file = "filename.txt"
File.open(file) do |sFile|
while line = sFile.gets
row = line.split("\t")
a_product = Product.new(
:sku => row[0] ,
:name => row[1] ,
:price => row[2] ,
a_product.save
end
end

What if I want to jump over the first line of the file so it won´t be
imported?

Thank you,

Mark

The $. variable holds the line number so you could do something like
this:

File.open("filename.txt") do |aFile|
aFile.each_line do |line|
if $. > 1
#processs data here

end
end
end


Luis
 
J

Joe

I don't see File.open here: http://ruby-doc.org/core/classes/File.html
Am I looking in the wrong place?

Also, how do you find out about globals like $. ?

Joe

I have this code:

file =3D "filename.txt"
File.open(file) do |sFile|
while line =3D sFile.gets
row =3D line.split("\t")
a_product =3D Product.new(
:sku =3D> row[0] ,
:name =3D> row[1] ,
:price =3D> row[2] ,
a_product.save
end
end

What if I want to jump over the first line of the file so it won=B4t be
imported?

Thank you,

Mark

The $. variable holds the line number so you could do something like
this:

File.open("filename.txt") do |aFile|
aFile.each_line do |line|
if $. > 1
#processs data here

end
end
end


Luis
 
L

lrlebron

I don't see File.open here:http://ruby-doc.org/core/classes/File.html
Am I looking in the wrong place?

Also, how do you find out about globals like $. ?

Joe

I have this code:
file = "filename.txt"
File.open(file) do |sFile|
while line = sFile.gets
row = line.split("\t")
a_product = Product.new(
:sku => row[0] ,
:name => row[1] ,
:price => row[2] ,
a_product.save
end
end
What if I want to jump over the first line of the file so it won´t be
imported?
Thank you,
Mark
The $. variable holds the line number so you could do something like
this:
File.open("filename.txt") do |aFile|
aFile.each_line do |line|
if $. > 1
#processs data here

Luis

Open is a kernel method
http://ruby-doc.org/core/classes/Kernel.html#M005990

For variables like $. see Programming Ruby 2ed Chapter 22 (page 334
for $.)

Luis
 
J

Joe

How would I know that File.open exists if I didn't look at Kernel?
The way Mark was using it, it made it look like a member of the File
class. Is this just something you have to know, or am I missing
something in ruby doc? I would think
http://ruby-doc.org/core/classes/File.html would list all members.

Is the problem that http://ruby-doc.org/core/classes/File.html doesn't
list members of a superclass?

Joe

I don't see File.open here:http://ruby-doc.org/core/classes/File.html
Am I looking in the wrong place?

Also, how do you find out about globals like $. ?

Joe
I have this code:
file =3D "filename.txt"
File.open(file) do |sFile|
while line =3D sFile.gets
row =3D line.split("\t")
a_product =3D Product.new(
:sku =3D> row[0] ,
:name =3D> row[1] ,
:price =3D> row[2] ,
a_product.save
end
end
What if I want to jump over the first line of the file so it won=B4= t be
imported?
Thank you,
The $. variable holds the line number so you could do something like
this:
File.open("filename.txt") do |aFile|
aFile.each_line do |line|
if $. > 1
#processs data here

Luis

Open is a kernel method
http://ruby-doc.org/core/classes/Kernel.html#M005990

For variables like $. see Programming Ruby 2ed Chapter 22 (page 334
for $.)

Luis
 
J

Joe

Thanks. That's what i was missing.

Joe

Exactly. It does however list which class is the superclass (IO) and if you
click on that you will see the method Io_Open which is the same as File.open.

HTH,
Sebastian
 
J

James Gray

Okeeeey, so you mean like this?:

No. To jump over a line do:

#!/usr/bin/env ruby -wKU

File.open(__FILE__) do |file| # open this code file
file.gets # read and skip one line
file.each do |line|
puts line # replace this with the needed code
end
end

__END__

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,786
Messages
2,569,626
Members
45,328
Latest member
66Teonna9

Latest Threads

Top