Read from variable instead of text file

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
scriptor2016
Posts: 862
Joined: 21 Dec 2015, 02:34

Read from variable instead of text file

15 Apr 2024, 15:07

Hi all

This is going to be a complicated question but I'll do my best...


c:\MyTextFile.txt contains:

Code: Select all

one
two
three
four
five
six
seven
eight
nine
ten
When scrolling with WheelUp or WheelDown, the code reads each individual line of text in the file and displays the data as a tooltip. For example:

A single click of WheelDown will show a tooltip saying "one". Another click of WheelDown will show a tooltip saying "two". A third click shows "three" and so on.

The same for WheelUp, except is goes upwards. So on each WheelUp click it will show a tooltip of "ten", "nine", "eight" and so on.

When the clicks hit each end (meaning when the click reaches "one" on one end or "ten" on the other, it will not wrap around- it hits the boundary at either end (and yes this is the intended action). So if it's at "ten", the next click says "ten" again and so on. Same for "one".

So basically the WheelUp and WheelDown clicks will go up and down the text file and not go past the boundary on either end.


Which leads the the question: right now the code is always reading from the text file on each WheelUp/WheelDown click.

How would I go about loading the text file into a variable first and then reading each line from this variable instead of the file? Because right now it's a little slow when having to read from the hard disk every time a WheelUp/WheelDown event occurs. If it was reading from the variable instead then I'm guessing it might speed things up quite a bit? Here's the code (which I didn't write, someone was nice enough to provide this a few years back):

Code: Select all

check = 0
fileread,content,c:\MyTextFile.txt
arr:=strsplit(content,"`r","`n")
cnt:= arr.length()

wheeldown:: 
upwards:=% ++check > cnt ? "Out-of-bounds" : arr[check]
;msgbox, % --check < 1 ? "Out-of-bounds" : arr[check]
FileReadLine, Line_Contents, c:\MyTextFile.txt, %check%
tooltip, %upwards% and the line content is %Line_Contents%
sleep, 50
check := Min(check,cnt+1)
return


wheelup:: 
downwards:=% --check < 1 ? "Out-of-bounds" : arr[check]
FileReadLine, Line_Contents, c:\MyTextFile.txt, %check%
tooltip, %downwards% and the line content is %Line_Contents%
sleep, 50
check := Max(check,0)
return
If anyone has read this far then thanks so much lol. I have a few things I've tried but none of them are working. I could post them but what's the point. I know that FileRead and Loop, Parse might have something to do with it but honestly it's over my head right now. Any help would be great, thanks again :)
User avatar
mikeyww
Posts: 27010
Joined: 09 Sep 2014, 18:38

Re: Read from variable instead of text file

15 Apr 2024, 15:50

One answer is StrSplit, which will split your text into an array, so that you can access each array element individually. It is analogous to your FileReadLine in that regard.

Wrap:

Code: Select all

#Requires AutoHotkey v1.1.33.11
str := "
(
one
two
three
four
five
six
seven
eight
nine
ten
)"
line := StrSplit(str, "`n", "`r")
WheelUp::  ToolTip % line[n := --n ? n : line.Count()]
WheelDown::ToolTip % line[n := n++ - line.Count() ? n : 1]
No wrap:

Code: Select all

n := 0, line := StrSplit(str, "`n", "`r")
WheelUp::  ToolTip % line[n := Max(1, n - 1)]
WheelDown::ToolTip % line[n := Min(line.Count(), n + 1)]
scriptor2016
Posts: 862
Joined: 21 Dec 2015, 02:34

Re: Read from variable instead of text file

15 Apr 2024, 16:38

thanks mikeyww! It works :)

Question- how do I load the text file in as the variable instead of pre-defining it inside the () brackets as your code is right now?
scriptor2016
Posts: 862
Joined: 21 Dec 2015, 02:34

Re: Read from variable instead of text file

15 Apr 2024, 16:42

actually I think I got it:

Code: Select all

fileread, str, C:\MyTest.txt

line := StrSplit(str, "`n", "`r")
n := 0, line := StrSplit(str, "`n", "`r")
WheelUp::  ToolTip % line[n := Max(1, n - 1)]
WheelDown::ToolTip % line[n := Min(line.Count(), n + 1)]
Is this now reading from the variable instead of the hard drive on each WheelUp/Down event?
User avatar
mikeyww
Posts: 27010
Joined: 09 Sep 2014, 18:38

Re: Read from variable instead of text file

15 Apr 2024, 16:55

Yes. "Line" is an array in memory.
scriptor2016
Posts: 862
Joined: 21 Dec 2015, 02:34

Re: Read from variable instead of text file

15 Apr 2024, 17:30

wow, this is awesome. It works perfectly!

I'm going to keep working with this and develop it more into my code.

Curious, how would I pop up a messagebox when it hits either boundary ("one" or "ten"), something like, msgbox Upper Limit Reached! or msgbox, Lower Limit Reached!

I'm guessing it has something to do with either the Min or Max words in the code... typically I'll just keep trial-ing and error-ing it until something works but it usually takes me all night lol
User avatar
mikeyww
Posts: 27010
Joined: 09 Sep 2014, 18:38

Re: Read from variable instead of text file

15 Apr 2024, 19:28

Code: Select all

WheelUp::
ToolTip % line[n := Max(1, n - 1)]
If (n = 1)
 MsgBox 48, Warning, Lower limit reached!
Return
scriptor2016
Posts: 862
Joined: 21 Dec 2015, 02:34

Re: Read from variable instead of text file

15 Apr 2024, 20:31

Perfect, thanks a lot!! Your help is much appreciated. I actually tried that syntax but I forgot to include the brackets so it didn't work. Oh well lesson learned. I'll keep this topic going if I come across any other issues with the code. :)
scriptor2016
Posts: 862
Joined: 21 Dec 2015, 02:34

Re: Read from variable instead of text file

16 Apr 2024, 17:39

So here I am working on this code some more (a little project I have going) and I've come to realise that I'd like to reverse the contents of "str" immediately after it's loaded in from the file. So in other words the text file would be upside down (or find a way to make "str" upside down, if that makes sense). I'll start researching how to do this now..
scriptor2016
Posts: 862
Joined: 21 Dec 2015, 02:34

Re: Read from variable instead of text file

16 Apr 2024, 17:58

Found a simple solution on the forums.

Code: Select all

fileread, str, C:\MyTestFile.txt
s:=StringReverse(str, "`n")
msgbox, %s%
Return


StringReverse(str, Separator="", OmitChars="") {
   Loop,Parse,% StrReplace(str,Separator,(BS:=chr(8))),%BS%,%OmitChars%
      i := A_LoopField (a_index=1 ? "" : BS i)
   Return StrReplace(i, BS, Separator)
}  ; A|B|CD --> CD|B|A
crazy how easy this was.. now all the lines in the variable are reversed as needed.. so instead of str being

1
2
3
4
5
6
7
8
9


it's


9
8
7
6
5
4
3
2
1

(as an example)

:)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: DaveF, Spawnova and 404 guests