Getting a line from a text file with a variable

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
UtahCode197
Posts: 21
Joined: Sat Jun 22, 2013 9:36 pm

Getting a line from a text file with a variable

Post by UtahCode197 » Wed Jun 10, 2015 5:30 pm

Hi,
Is there a way of getting a certain line set by a variable and putting it into a field.
I've got the code below but it returns a compiling error:

Code: Select all

put line sVariable of URL "file:/list.txt" into field "result"
And also...is there a way to store the amount of lines that are in a file as a variable?

Thanks

Klaus
Posts: 14187
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Getting a line from a text file with a variable

Post by Klaus » Wed Jun 10, 2015 5:49 pm

Hi Rory,

hm, the script looks OK!? Sure the pathname is correct?
The leading / is puzzling me a bit and try with an absolute pathname.

Just made a test and this works fine:

Code: Select all

on mouseUp
   put specialfolderpath("desktop") & "/testtext.txt" into tFile
   put 2 into sVariable
   put line sVariable of URL ("file:" & tFile) 
end mouseUp
Got the correct line in the message box!

Here the answer to your questions:

Code: Select all

on mouseUp
   ## Add your pathname here:
   put specialfolderpath("desktop") & "/testtext.txt" into tFile

   ## Read the file completely!
   put URL ("file:" & tFile) into tCompleteFile

   ## Now extract your line:
   put 2 into sVariable
   put line sVariable of tCompleteFile into fld "result"

   ## Put the number of lines into the variable tNumOfLines:
   put the num of lines of tCompleteFile into tNumOfLines
end mouseUp
Best

Klaus

UtahCode197
Posts: 21
Joined: Sat Jun 22, 2013 9:36 pm

Re: Getting a line from a text file with a variable

Post by UtahCode197 » Wed Jun 10, 2015 6:03 pm

I think I've spotted the problem. I was trying to put the thumbPosition of a scrollbar as the value of sVariable but I think it hasn't worked. How would I do this. I've just tried setting sVariable to thumbPosition in code under the scrollbar and then using sVariable as you've shown me but it's not working. Do you know how I can make this work?

Thanks so far :D

Klaus
Posts: 14187
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Getting a line from a text file with a variable

Post by Klaus » Wed Jun 10, 2015 6:21 pm

Hi Rory,

...
put the thumbpos of sb "your scrollbar here..." into sVariable
...
should do the trick!

Hint:
I use a lot of valid abbreviations, lazy moi :D
cd = card
sb = scrollbar
btn = button
grp = group
grc = graphic
bg = background
fld = field


Best

Klaus

P.S.
Here some really great learning resources for the basics of Livecode:
http://www.hyperactivesw.com/revscriptc ... ences.html

SparkOut
Posts: 2944
Joined: Sun Sep 23, 2007 4:58 pm

Re: Getting a line from a text file with a variable

Post by SparkOut » Wed Jun 10, 2015 8:24 pm

The thumbpos may not be an integer, therefore you could not (say) get line 1.7 of the file.
If you round the thumbpos value in the variable, does that work?

Post Reply