notHalfBad
notHalfBad

Reputation: 213

How to separate a file of words into a list in LiveCode

I'm really new to LiveCode and I have a file of words, each one on a new line. I want to write these into a list variable in LiveCode, so that I can choose one of them at random later. If I were to do this in python, it would look something like this:

    list1 = []
    with open('words.txt') as f:
        for line in f:
            list1.append(line.strip())

However, I am unsure of how to do this in LiveCode and have been unable to find anything telling me how. Help is much appreciated.

Upvotes: 0

Views: 89

Answers (2)

Mark
Mark

Reputation: 2435

There is an easy way to do what you want. Assume that myFile contains the path to your file.

put url ("binfile:" & myFile) into myList
replace crlf with lf in myList
replace numToChar(13) with lf in myList
put any line of myList into myWord

The variable myWord now contains a random line (word) from your file.

Upvotes: 0

Scott Rossi
Scott Rossi

Reputation: 885

You can place the contents of an external text file into a variable like this:

put url ("file:words.txt") into list1

Use the word "file" to tell LiveCode that you want to access the external file as text, rather binary, in which case you'd use "binfile:".

Upvotes: 1

Related Questions