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

时间:2016-01-11 13:46:54

标签: arrays external smallbasic

我试图在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)

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

2 个答案:

答案 0 :(得分:0)

你可以使用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

答案 1 :(得分:0)

我知道我可能已经太晚了但是如果你还在继续(是的,我会假设你没有参加计算机科学的AQA GCSE但更喜欢在Small Basic中编写代码以获得乐趣),但您应该考虑使用此代码,因为这样效率更高。

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

(您需要将fpath变量更改为文件所在的位置)。然后,这也打印出阵列只是为了检查,但为了你的任务,你需要摆脱它。