0

I'm trying to bring in a simple list of 10 words (without commas) on 10 lines and save them as a list or array in Small Basic.

I know I need to loop through all the lines in the file but I can only get it to do it with individual letters.

I've got this far so far

OpenFile = File.ReadContents("example.txt")
For i = 1 To Text.GetLength(OpenFile)
    WordList[i] = Text.GetSubText(OpenFile, i, 5)
EndFor
TextWindow.Write(WordList)

I haven't got any further than this and not sure where to go to from here.

2 Answers2

0

YOu could use readline to get all the characters/words/sentence in a line, this is an example, its not complete but it gives the idea of what it is you need,

'SAVE THE PROGRAM FIRST!!!!! 
'DO NOT RUN UNTIL YOU DO THAT.
makesaves()
getsaves()
printsaves()
Sub makesaves ' where you make saves. its reusable. 
PATH = Program.Directory + "\animals\" ' SAVE THIS IN A FOLDER YOU WILL FIND IN 
'ELSE IT WILL SAVE IN A DUMP FOLDER WHICH IS NEARLY IMPOSSIBLE TO FIND
NAME = "Animal"
EXT = ".txt"

filesave["1"] = "Cheetah"
filesave[2] = "horse"
filesave[3] = "dog"
filesave[4] = "cat"
filesave[5] = "mouse"
filesave[6] = "turtle" 
filesave[7] ="Bird"
filesave[8] = "snake"
filesave[9] = "snail"
filesave[10] = "Rat"
'makes the saves
File.CreateDirectory(PATH) ' makes the path.
File.WriteContents(PATH+NAME+EXT, filesave)

filesave = "" ' cleans the file so you dont get repeats. e.i. - save dog.    read dog, save dog, read dog dog.
'this makes it so you see dog once. its an override.
 filesave = File.ReadContents(PATH + NAME + EXT)  'reads the content 

endsub

Sub getsaves
filesave = File.ReadContents(PATH+NAME+EXT) ' how this writes is cheetah;   horse; 

cheetah = filesave[1]
horse = filesave[2]
dog = filesave[3] 
cat = filesave[4]
mouse = filesave[5] 'mouse and turtle as different color because they can be   used as functions. ignore
turtle = filesave[6]  
bird = filesave[7]
 snake = filesave[8]
 snail = filesave[9] 
 rat = filesave[10] 


EndSub
Sub printsaves
i = 1
While i < 11 
  TextWindow.WriteLine(filesave[i])
i = i+1
endwhile


endsub
Matthew
  • 157
  • 1
  • 9
  • Still struggling to get my head around it. Could you show me an example as if it was a reading a list of 10 animals? Thanks – mills-potter Jan 11 '16 at 20:47
0

I know I'm probably much too late but in case you're still going (and yes, I'm going to assume that you're not taking an AQA GCSE in Computer Science but instead like to code in Small Basic for fun), but you should be looking at using this code instead as this is much more efficient.

fpath = "\\Downloads\file.txt"

For i = 1 To 10
  line[i] = File.ReadLine(fpath, i) 
EndFor

For i = 1 To 10
  TextWindow.WriteLine("Line " + i + " contains: " + line[i])
EndFor

(You'll need to change the fpath variable to wherever your file is). This then also prints out the array just to check but for your task you'll need to get rid of that.