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
Reading text files
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Re: Reading text files
This might get you started
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
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
Craig Newman
Re: Reading text files
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
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
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w