Small Basic:如何将外部文本文件中的单词拆分为列表



我试图在10行中引入一个由10个单词(不带逗号)组成的简单列表,并将它们保存为Small Basic中的列表或数组。

我知道我需要循环浏览文件中的所有行,但我只能用单个字母来完成。

到目前为止,我已经完成了

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

我没有比这更进一步的了,也不知道从这里去哪里。

YOu可以使用readline来获取一行中的所有字符/单词/句子,这是一个例子,它并不完整,但它给出了你需要的东西的想法,

'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

我知道我可能已经太晚了,但如果你还在继续的话(是的,我假设你不是在参加计算机科学的AQA GCSE考试,而是喜欢用Small Basic编程取乐),但你应该考虑使用这个代码,因为这更有效。

fpath = "\Downloadsfile.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

(您需要将fpath变量更改为文件所在的位置)。这也打印出数组只是为了检查,但对于您的任务,您需要去掉它。

相关内容

  • 没有找到相关文章

最新更新