Reading text files

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
carel
Posts: 58
Joined: Mon Jul 29, 2013 2:49 pm

Reading text files

Post by carel » Wed Jul 31, 2013 5:14 pm

Hi,

I have a text file like this:

question*answer1*answer2*answer3*answer4*answer5*3 (the 3 indicating which is the correct answer - but this is not relevant)

(the file contains hundreds of lines)

What is the best way to get it into arrays?

I want it like this:

question[1 to 100] (let's assume there's 100 questions)
answer1[1..100] etc.

or

myArray[1][1] = question
myArray[1][2] = answer1
myArray[1][3] = answer2 and so on (is that even how you express multidimensional arrays) or would it be myArray[1,1] ?

I was thinking of doing the following:

put 1 into mycounter
repeat
read from file "myfille.txt" until "*"
put it into question[mycounter]
read from file "myfile.txt" until "*"
put it into answer1[mycounter]
etc...
read from file "myfile.txt" until return (after I read all the answers)
mycounter = mycounter + 1
until eof "myfile.txt"

I don't mind reading the whole file into a text variable and then do the parsing on that (I just don't know how to do that either)

Thanks,

Carel

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

Re: Reading text files

Post by dunbarx » Wed Jul 31, 2013 6:34 pm

This might get you started

Code: Select all

on mouseUp
   put "questionA*answer1a*answer2a*answer3a*answer4a*answer5a*3" into line 1 of temp
   put "questionB*answer1b*answer2b*answer3b*answer4b*answer5b*3" into line 2 of temp
   put "questionC*answer1c*answer2c*answer3c*answer4c*answer5c*3" into line 3 of temp
   set the itemdel to "*"
   
   repeat with u = 1 to the number of lines of temp
      repeat with y = 2 to 7
         put item y of line u of temp into myArray[item 1 of line u of temp][y]
      end repeat
   end repeat
   
   answer myArray["questionB"][3]
end mouseUp
Not sure how you want your data back. Experimentation is the best way to learn arrays. Put breakpoints in your scripts and see how they develop.

Craig Newman

carel
Posts: 58
Joined: Mon Jul 29, 2013 2:49 pm

Re: Reading text files

Post by carel » Mon Aug 12, 2013 5:16 pm

Thanks Craig

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Reading text files

Post by MaxV » Wed Aug 21, 2013 3:12 pm

Don't you like split? See http://docs.runrev.com/Command/split

However Livecode mebed SQLite, so you can import, create and use a database, see: http://livecode.wikia.com/wiki/SQLite
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

Post Reply