Read file line for line?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Garrett
Posts: 386
Joined: Sat Apr 08, 2006 8:15 am
Contact:

Read file line for line?

Post by Garrett » Tue Sep 08, 2009 6:53 pm

I'm missing something here I'm sure. I'm trying to read a file line for line, but can't figure out how to tell Rev to move to the next line in the file.

Code: Select all

   put "...Downloads\enwiki-20090902-pages-articles.xml\enwiki-20090902-pages-articles.xml" into varFileToRead
   open file varFileToRead for read
   put 1 into varCounter
   put "filler" into varLineData
   --repeat until varLineData is empty
   read from file varFileToRead for 1 line
   put it into varLineData
   --end repeat
   close file varFileToRead
I'm using "read from file varFileToRead for 1 line" but see no way to tell Rev what line to read, unless it's not possible to use "line" in this manner, I did notice that it was shown as an example for "stdin" and not "file". If not possible, then I know I'll have to play a bit of a guessing game at using character counting to do this.

Any suggestions/help appreciated.

Thanks,
~Garrett
'What you do not want done to yourself, do not do to others.' - Confucius (550 b.c. to 479 b.c.)

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Tue Sep 08, 2009 8:09 pm

When you use

Code: Select all

read from file theFilePath for 1 line
the engine reads until it finds the first linefeed. You have to keep track of where you're at; even though there's a seek command to jump to a specific spot in the file or some relative distance from where you're at, there's no seekPosition function to tell you where you currently are.

Best of luck,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

Garrett
Posts: 386
Joined: Sat Apr 08, 2006 8:15 am
Contact:

Post by Garrett » Tue Sep 08, 2009 9:06 pm

Ok, so I could do something like this then:

Code: Select all

seek varLineCounter in file varFileToRead
Basically, increment the "varLineCounter" myself so I know which line I'm on.

Sweet! That's what I needed, but of course, was looking for the wrong keyword in the docs....

Thanks again Jan! :D
'What you do not want done to yourself, do not do to others.' - Confucius (550 b.c. to 479 b.c.)

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Wed Sep 09, 2009 5:51 am

The seek command doesn't think in terms of lines, but in bytes/characters. Also bear in mind that read ... for 1 line may result in your slurping up the entire file, if the file doesn't contain linefeeds (as can be the case with XML files).

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

Post Reply