Author Topic: Read a text file and move back and forward in it  (Read 4088 times)

JohnMcClane3264

  • Guest
Read a text file and move back and forward in it
« on: October 29, 2017, 03:04:00 PM »
I would like to do the following (if it has been mentioned somewhere before please point me to it, I have not found a forum topic covering it):

- Read a file containing multiple lines (either into an "array", which I know does not exists as native data structure or sequentially till I hit line nr. X which I want to process further)
- Working with the file by moving 1 line forward or backward upon certain commands and let the TTS read that line to me or do something else with this line's content.

Can that be done with a stock version of voice attack?

Why do I want to do this? (Maybe someone has a different approach to that kind of problem)
I want to create a list of Star Systems in Elite Dangerous to visit one by one, I want to create a text file with the system names and planet names and traverse it via Voice commands (keeping an "index" to know where I am and then move forward or backward from there). The current system should be typed into the galaxy map search text box when I say a certain command. I guess I can figure out all the latter stuff, I just need help in how to read the file either sequentially or in some kind of array.

Pseudo Code of what I'd like to do:
File "StarSystems.txt"
Code: [Select]
1. Sol
P 3
P 4
P 5
2. Wolf 359
P 1
P 2
3.Some other System
P A1
P B2

Commands (pseudo code):

Code: [Select]
Start at Star number <X> : Positions my "index" at the text line which is the Xth star in my file.
Go to next planet : Positions the "index" one line down and checks if it is starting with "P" to indicate there are more planets and tells me the planet name
Go to previous planet : Positions the "index" one line up and does the same as the above command
Go to next star : Continues to read after the current "index" position and stops at the next line that starts with a number
Go to last star: Reads the file (most likely from the beginning since I don't think there is any moving back within files) to the star above the one I was just using.
Type current star name : Sends each char of the current star as a single keystroke (for the galaxy map search text box), so if I am at Nr. 2
it would send "W" "o" "l" "f" " " "3" "5" "9" (this seems easy to do with substr and length functions, so I don't really see a problem here)

Any help with this little project would be much appreciated!

Gangrel

  • Caffeine Fulled Mod
  • Global Moderator
  • Full Member
  • *****
  • Posts: 216
  • BORK FNORK BORD
Re: Read a text file and move back and forward in it
« Reply #1 on: October 29, 2017, 07:06:01 PM »
I did something similar to this in Autohotkey (to read the file, progress to next one in list), and used Voice Attack to press the relevant button combo to copy the line into clipboard, and then get VA to open the map and then Ctrl+V the destination into the box).

Mine did a bit more as it kept track of where it was between sessions, so all I had to do was load it up (select the route I was on), and then away I go.

Base AHK code follows:

Code: [Select]
#IfWinActive ahk_exe EliteDangerous64.exe
SetWorkingDir %A_ScriptDir%
FileSelectFile, routefile, RootDir\Filename, *.txt
IniRead, i, route.ini, %routefile%, Number, 0

!F13::
FileReadLine, clipboard, %routefile%, ++i
StopNumber = %i%
IniWrite, %StopNumber%, route.ini, %routefile%, Number
Return

Should point out that this is just one direction (go forward), but with some planning, it can work backwards/reset route etc

Antaniserse

  • Global Moderator
  • Jr. Member
  • *****
  • Posts: 87
    • My VA plugins
Re: Read a text file and move back and forward in it
« Reply #2 on: October 30, 2017, 02:04:26 AM »
Are you bound to this specific file format, or is it something that you can edit on your own as you like?

Because if the latter, it would probably be much easier and faster to structure your data as a CSV/JSON/XML file, and the access the desired values with my plugin, or some equivalent inline code

Having said that, a (very crude) example of reading you data one line at a time could be:

Code: [Select]
Begin Integer Compare : [MyFileIndex] Has Not Been Set
    Set integer [MyFileIndex] value to 0
End Condition
Set Text [MyFileName] to 'StarSystems.txt'
Inline VB Function: Load File Lines
Say, '{TXT:LastReadLine}'

'You just need to manipulate this variable to go back and forth, wherever you like... here it's a simple 'next line'
Set integer [MyFileIndex] to [MyFileIndex] plus 1

where the inline code is

Code: [Select]
Public Class VAInline
    Public Sub Main()
            Dim fileLines() As String = IO.File.ReadAllLines(VA.GetText("MyFileName"))

            VA.SetText("LastReadLine", fileLines(VA.GetInt("MyFileIndex")))
    End Sub
End Class

Of course this is missing all the safety checks and it also reloads the file from disk every time, so it will need some work to get decent, but just to get you started
« Last Edit: October 30, 2017, 02:09:49 AM by Antaniserse »
"I am not perfect and neither was my career. In the end tennis is like life, messy."
Marat Safin