need help about variable(about assign global and default value) Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
cunningwolf
Posts: 5
Joined: 07 Apr 2024, 09:10

need help about variable(about assign global and default value)  Topic is solved

12 Apr 2024, 05:34

i had a txt file:
--------------------

Code: Select all

re	//words start with re...
return
rest
remove

str	//AHK script: about string...
StrReplace(A_Clipboard, "``r``n")
if InStr(txt0, "searching string")
...ect
--------------

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance force

;re := ""
txt0 := "re	//words start with re..."   ;use loop and strSplit got this line
test()

;#Include "hotKey.ahk2"
:?X:re::showMenu("re")		;fileAppend to other *.ahk and attach here
;:?X:str::showMenu("str")


test(*) {
global
hs0 := StrSplit(txt0, "`t")

/*-------------
;global %hs0[1]% := hs0[2]	;error
;%hs0[1]% := hs0[2]
;MsgBox re
--------*/

;why %hs0[1]% can't default the value and
;how to assign %hs0[1]% as a global variable to be use outoutside the {}
%hs0[1]% := menu()
%hs0[1]%.Add("return", pasteMenu)
%hs0[1]%.Add()
%hs0[1]%.Add("rest", pasteMenu)
%hs0[1]%.Add("remove", pasteMenu)
}



showMenu(M0) {
  CaretGetPos(&x0, &y0)
  if (x0 = "" or y0 = "") {
    MouseGetPos &x0, &y0 
  } else  {
  x0 := x0 - 15
  y0 += 50
	}
  sleep 500
  %M0%.Show(x0, y0)
}

pasteMenu(x0, *) {
  A_Clipboard := x0
  sleep 20
  Send "^v"
  }


ps. if i add
re := ""
at the very begining, the script works.


[Mod edit: Added code box for text file so users can actually see where the tab characters are.]
User avatar
Seven0528
Posts: 347
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: need help about variable(about assign global and default value)

12 Apr 2024, 06:04

 I have a feeling that the variable re might be used somewhere in the script, but I'm not sure from the code you provided.

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
:?X:re::function() ;  No problem here.
function()    {

}
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
User avatar
boiler
Posts: 16996
Joined: 21 Dec 2014, 02:44

Re: need help about variable(about assign global and default value)

12 Apr 2024, 06:46

Seven0528 wrote:  I have a feeling that the variable re might be used somewhere in the script, but I'm not sure from the code you provided.
It is — from the StrSplit of the text from the text file, which then gets assigned as the first element of the hs0 array, then hs0[1] gets dereferenced to be used as a variable name, causing a warning/error unless it is first initialized.
cunningwolf
Posts: 5
Joined: 07 Apr 2024, 09:10

Re: need help about variable(about assign global and default value)

12 Apr 2024, 06:59

@boileryes, you are right, the re is fist get by: hs0 := StrSplit(txt0, "`t"), variable hs0[1] is the re. the problem is, AHK need to assign re as global and default value before it. i'm a V2 newbie, does anyone know how to correct/solve the problem? thank you all.
User avatar
Seven0528
Posts: 347
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: need help about variable(about assign global and default value)

12 Apr 2024, 07:06

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
#Warn VarUnset, Off

text := "var`tstr"
data := strSplit(text,"`t")
%data[1]% := data[2]
msgbox var

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force

text := "var`tstr"
data := strSplit(text,"`t")
%data[1]% := data[2]
msgbox var?
Last edited by Seven0528 on 12 Apr 2024, 07:19, edited 1 time in total.
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
User avatar
boiler
Posts: 16996
Joined: 21 Dec 2014, 02:44

Re: need help about variable(about assign global and default value)

12 Apr 2024, 07:07

Basically, v2 deters using dynamic variable names in this manner. You should be able to accomplish your goal using something like a Map object (associative array).
User avatar
Seven0528
Posts: 347
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: need help about variable(about assign global and default value)

12 Apr 2024, 07:08

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force

obj := {}
text := "prop`tstr"
data := strSplit(text,"`t")
obj.%data[1]% := data[2]
msgbox obj.prop

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force

obj := map()
text := "key`tstr"
data := strSplit(text,"`t")
obj[data[1]] := data[2]
msgbox obj["key"]

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force

class cl
{
}
text := "prop`tstr"
data := strSplit(text,"`t")
cl.%data[1]% := data[2]
msgbox cl.prop
 I mistook almost all of the code being commented out as if you were asking about Hotstrings. I'm sorry.
Last edited by Seven0528 on 12 Apr 2024, 07:45, edited 2 times in total.
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
gregster
Posts: 9036
Joined: 30 Sep 2013, 06:48

Re: need help about variable(about assign global and default value)

12 Apr 2024, 07:28

@Seven0528
Yeah, the codeboxes still work with v1 syntax and the multi-line comment syntax changed in v2. This can be misleading.
cunningwolf
Posts: 5
Joined: 07 Apr 2024, 09:10

Re: need help about variable(about assign global and default value)

12 Apr 2024, 22:38

thanks you so much guys. Learn a lot. I am success correct my code using map() method.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: FalseShepard, mikeyww, vmech and 83 guests