Page 1 of 1

Reading text files

Posted: Wed Jul 31, 2013 5:14 pm
by carel
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

Re: Reading text files

Posted: Wed Jul 31, 2013 6:34 pm
by dunbarx
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

Re: Reading text files

Posted: Mon Aug 12, 2013 5:16 pm
by carel
Thanks Craig

Re: Reading text files

Posted: Wed Aug 21, 2013 3:12 pm
by MaxV
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