[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Mark Toth

12/28/2007 6:50:00 PM

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
--
Posted via http://www.ruby-....

13 Answers

Thomas Wieczorek

12/28/2007 7:05:00 PM

0

On Dec 28, 2007 7:49 PM, Mark Toth <mark.toth@telia.com> wrote:
>
> What if I want to jump over the first line of the file so it won=B4t be
> imported?
>

You could use FasterCSV with the :header option set to true.
It is a pretty fast gem to read X separated files:
http://fastercsv.ruby...

Stefano Crocco

12/28/2007 7:06:00 PM

0

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

Mark Toth

12/28/2007 7:12:00 PM

0

Stefano Crocco wrote:
> 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
--
Posted via http://www.ruby-....

Thomas Wieczorek

12/28/2007 7:16:00 PM

0

On Dec 28, 2007 8:12 PM, Mark Toth <mark.toth@telia.com> wrote:
> Stefano Crocco wrote:
> Hmmm, it sounds like a great idea! Can you please send me some code for
> it?
>

Just add a sFile.gets before the while.

Mark Toth

12/28/2007 7:59:00 PM

0

Thomas Wieczorek wrote:
> On Dec 28, 2007 8:12 PM, Mark Toth <mark.toth@telia.com> wrote:
>> Stefano Crocco wrote:
>> Hmmm, it sounds like a great idea! Can you please send me some code for
>> it?
>>
>
> 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
--
Posted via http://www.ruby-....

Lee Jarvis

12/28/2007 8:09:00 PM

0

File#foreach anyone?

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

Regards,
Lee
--
Posted via http://www.ruby-....

lrlebron@gmail.com

12/28/2007 8:18:00 PM

0

On Dec 28, 12:49 pm, Mark Toth <mark.t...@telia.com> wrote:
> 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
> --
> Posted viahttp://www.ruby-....

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

Joe

12/28/2007 9:01:00 PM

0

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

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

Joe

On Dec 28, 2007 3:20 PM, lrlebron@gmail.com <lrlebron@gmail.com> wrote:
>
> On Dec 28, 12:49 pm, Mark Toth <mark.t...@telia.com> wrote:
> > 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
> > --
> > Posted viahttp://www.ruby-....
>
> 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
>
>
>

lrlebron@gmail.com

12/28/2007 9:34:00 PM

0

On Dec 28, 3:01 pm, Joe <qbpro...@gmail.com> wrote:
> I don't see File.open here:http://ruby-doc.org/core/classes...
> Am I looking in the wrong place?
>
> Also, how do you find out about globals like $. ?
>
> Joe
>
> On Dec 28, 2007 3:20 PM, lrleb...@gmail.com <lrleb...@gmail.com> wrote:
>
>
>
> > On Dec 28, 12:49 pm, Mark Toth <mark.t...@telia.com> wrote:
> > > 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
> > > --
> > > Posted viahttp://www.ruby-....
>
> > 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

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

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

Luis

Joe

12/29/2007 12:26:00 AM

0

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... would list all members.

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

Joe

On Dec 28, 2007 4:35 PM, lrlebron@gmail.com <lrlebron@gmail.com> wrote:
> On Dec 28, 3:01 pm, Joe <qbpro...@gmail.com> wrote:
> > I don't see File.open here:http://ruby-doc.org/core/classes...
> > Am I looking in the wrong place?
> >
> > Also, how do you find out about globals like $. ?
> >
> > Joe
> >
>
> > On Dec 28, 2007 3:20 PM, lrleb...@gmail.com <lrleb...@gmail.com> wrote:
> >
> >
> >
> > > On Dec 28, 12:49 pm, Mark Toth <mark.t...@telia.com> wrote:
> > > > 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,
> >
> > > > Mark
> > > > --
> > > > Posted viahttp://www.ruby-....
> >
> > > 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
>
> Open is a kernel method
> http://ruby-doc.org/core/classes/Kernel.ht...
>
> For variables like $. see Programming Ruby 2ed Chapter 22 (page 334
> for $.)
>
> Luis
>
>