Page 1 of 1

Getting a line from a text file with a variable

Posted: Wed Jun 10, 2015 5:30 pm
by UtahCode197
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

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

Posted: Wed Jun 10, 2015 5:49 pm
by Klaus
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

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

Posted: Wed Jun 10, 2015 6:03 pm
by UtahCode197
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

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

Posted: Wed Jun 10, 2015 6:21 pm
by Klaus
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

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

Posted: Wed Jun 10, 2015 8:24 pm
by SparkOut
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?