basic help - reading strings

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
pucker22
Posts: 2
Joined: Sat Jun 28, 2008 1:55 am

basic help - reading strings

Post by pucker22 » Sun May 23, 2010 11:43 pm

Hi,

How do you read from a file and place that text into a field, I tried this with no luck:-

Code: Select all

read from file"C:\Users\Andrew\Desktop\demo.txt" for 1 and put into field 'Field'
Also how do you read multiple lines? How do you identify each line with its own string such as line1$ line2$ line3$ etc etc....

Also how do you save multiple lines??

Sorry I have no clue I never used runrev much!

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: basic help - reading strings

Post by jmburnod » Mon May 24, 2010 10:47 am

Hi Pucker 22,

Try this :

Code: Select all

    put "C:\Users\Andrew\Desktop\demo.txt" into MyFile
    open file MyFile 
    read from file MyFile until eof
    close file MyFile
    put it into fld TheField
Best

Jean-Marc
https://alternatic.ch

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

Re: basic help - reading strings

Post by Klaus » Mon May 24, 2010 12:18 pm

Hi Pucker,

to avoid too much typing you could also use the "url" systax!

1. Read a text file and put it into a field:
(Hint: Rev always uses the UNIX path delimiter SLASH internally!)
...
put url("file:C:/Users/Andrew/Desktop/demo.txt") into field "Name of your field here"
...

2. You can also specify CHUNKS of text to be read:
...
## Only first three character
put CHAR 1 to 3 of url("file:C:/Users/Andrew/Desktop/demo.txt") into field "Name of your field here"
...
## Only line 1, 2 and 3
put line 1 to 3 of url("file:C:/Users/Andrew/Desktop/demo.txt") into field "Name of your field here"
...
## Last line of file
put line - 1 url("file:C:/Users/Andrew/Desktop/demo.txt") into field "Name of your field here"
...

3. To read non contiguous lines you need to read the whole file and then extract the lines (or characters) you need
## Read complete file into a variable
put url("file:C:/Users/Andrew/Desktop/demo.txt") into tVariable
put line 1 of tVariable & CR & line 4 of tVariable & CR & line 88 of tVariable into field "Name of your field here"
...

To wite content to a file do this the other way round :-)

1. Write content of variable to file
...
put tVariable into url("file:C:/Users/Andrew/Desktop/demo.txt")
...

2. Content of a field
...
put field "Your field" of cd 1 of stack "whatever stack the field is in" into url("file:C:/Users/Andrew/Desktop/demo.txt")
...

3. Non contiguous lines of variable oor field
put line 1 of tVariable & CR & line 4 of tVariable & CR & line 88 into url("file:C:/Users/Andrew/Desktop/demo.txt")
...

Hope that helps!


Best from germany

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: basic help - reading strings

Post by dunbarx » Mon May 24, 2010 2:46 pm

Hi.

But much more importantly, you need to first play around with a stack, making a few objects work in different ways. Your introductory post indicates that you need to become more familiar with the basic syntax of writing scripts.

Make a button that calculates the tax on an item. You will have fun making the interface and functionality work the way you (finally) like it to. Make an address book.

You will be amazed at how soon you will see what was wrong (basically, not conceptually) with your line of code.

And always ask for help.

Craig Newman

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

Re: basic help - reading strings

Post by Klaus » Mon May 24, 2010 3:13 pm

And don't forget to go through all these insanely great tutorials 8) :
http://www.runrev.com/developers/lesson ... nferences/


Best

Klaus

Post Reply