Function can not contain function, a way around? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Function can not contain function, a way around?

Post by Krd » 05 Oct 2022, 06:19

Hey folks!

I have this code which works fine in a loop with a hotkey.
But Now I want to make it all as a function to easily refer to this among several other functions one after one.
How to make the below to work?

Code: Select all

Fucntion_Test()    
{
	Clipboard := ""
	;Click, Click ect
    Sleep, 500
    Send, {Ctrl Down}c{Ctrl Up}
    ClipWait, 3
	if ErrorLevel
      {
      MsgBox, Error
      }
    cell(str, oneCellRange, digits := 0) {
    col  := Asc(Format("{:U}", RegExReplace(oneCellRange, "\d+"))) - 64
    row  := RegExReplace(oneCellRange, "\D+")
    line := StrSplit(str, "`n")
    cellVal := StrReplace(StrReplace(StrSplit(line[row], "`t")[col], " "), ",", ".")
    If cellVal is number
         Return Round(cellVal, digits)
    Else Return cellVal
}
MyVar := cell(Clipboard, "D1", 1)
   if (MyVar >= 10000)
      {
      MsgBox, % MyVar
      }
   else Continue
}

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Function can not contain function, a way around?

Post by mikeyww » 05 Oct 2022, 06:28

You can create two separate functions. One can call the other.

Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: Function can not contain function, a way around?

Post by Krd » 05 Oct 2022, 06:49

I tried this:

Code: Select all

cell(str, oneCellRange, digits := 0) {
    col  := Asc(Format("{:U}", RegExReplace(oneCellRange, "\d+"))) - 64
    row  := RegExReplace(oneCellRange, "\D+")
    line := StrSplit(str, "`n")
    cellVal := StrReplace(StrReplace(StrSplit(line[row], "`t")[col], " "), ",", ".")
    If cellVal is number
         Return Round(cellVal, digits)
    Else Return cellVal
MyVar := cell(Clipboard, "D1", 1)	
}
And I added this to the first part of the code:

Code: Select all

cell(str, oneCellRange, digits := 0) 
   if (MyVar >= 10000)
But cell is not getting a value. Correct the noob.

MyVar must be global to escape the cell function. How to let other functions to access its value? As Byref is now used for other stuff.

Tensai
Posts: 29
Joined: 12 Dec 2019, 14:15

Re: Function can not contain function, a way around?

Post by Tensai » 05 Oct 2022, 06:51

Autohotkey v1 you can use gosub within the function to try simulating nested functions or define separately like @mikeyww suggested.
Autohotkey v2 allows nested functions.
https://lexikos.github.io/v2/docs/Functions.htm#nested

Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: Function can not contain function, a way around?

Post by Krd » 05 Oct 2022, 06:55

I don't want to use goto or gosub :D

I don't have access to V2 :D

So the question is how to make this function use the clipboard content to do its math and return a variable which can be accessed by other functions :

Code: Select all

cell(str, oneCellRange, digits := 0) {
    col  := Asc(Format("{:U}", RegExReplace(oneCellRange, "\d+"))) - 64
    row  := RegExReplace(oneCellRange, "\D+")
    line := StrSplit(str, "`n")
    cellVal := StrReplace(StrReplace(StrSplit(line[row], "`t")[col], " "), ",", ".")
    If cellVal is number
         Return Round(cellVal, digits)
    Else Return cellVal
MyVar := cell(Clipboard, "D1", 1)	
}

Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: Function can not contain function, a way around?

Post by Krd » 05 Oct 2022, 07:20

Code: Select all

Fucntion_Test()    
{
	Clipboard := ""
	;Click, Click ect
    Sleep, 500
    Send, {Ctrl Down}c{Ctrl Up}
    ClipWait, 3
	if ErrorLevel
      {
      MsgBox, Error
      }
cell(str, oneCellRange, digits := 0) 
MyVar := cell(Clipboard, "D1", 1)
   if (MyVar >= 10000)
      {
      MsgBox, % MyVar
      }
   else Continue
}


cell(str, oneCellRange, digits := 0) 
{
    col  := Asc(Format("{:U}", RegExReplace(oneCellRange, "\d+"))) - 64
    row  := RegExReplace(oneCellRange, "\D+")
    line := StrSplit(str, "`n")
    cellVal := StrReplace(StrReplace(StrSplit(line[row], "`t")[col], " "), ",", ".")
    If cellVal is number
         Return Round(cellVal, digits)
    Else Return cellVal	
}
This works but now a warning, this variable has not been assigned a value. Im sure my way of doing it is not optimal.
image.png
image.png (2.29 KiB) Viewed 544 times

User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: Function can not contain function, a way around?

Post by boiler » 05 Oct 2022, 07:27

Calling the function like below from inside your test function doesn’t make sense:

Code: Select all

cell(str, oneCellRange, digits := 0)
You don’t have the variables str or oneCellRange in your test function (or digits, except for it being assigned a value inline there). You apparently just placed a copy of the function definition line in your test function for no purpose. Why did you put that there before calling it with actual values in the parameters (i.e., the proper way) on the following line? Just remove it.

Tensai
Posts: 29
Joined: 12 Dec 2019, 14:15

Re: Function can not contain function, a way around?  Topic is solved

Post by Tensai » 05 Oct 2022, 07:36

Code: Select all

Function_Test()
Function_Test() ; Not that it matters...but I think you want Function_Test() instead of Fucntion_Test() 
{
	Clipboard := ""
	;Click, Click ect
    Sleep, 500
    Send, {Ctrl Down}c{Ctrl Up}
    ClipWait, 3
	if ErrorLevel
      {
      MsgBox, Error
      }
; remove this line ; cell(str, oneCellRange, digits := 0)
MyVar := cell(Clipboard, "D1", 1)
   if (MyVar >= 10000)
      {
      MsgBox, % MyVar
      }
; continue must be enclosed by a loop
; else Continue
}

cell(str, oneCellRange, digits := 0)
{
    col  := Asc(Format("{:U}", RegExReplace(oneCellRange, "\d+"))) - 64
    row  := RegExReplace(oneCellRange, "\D+")
    line := StrSplit(str, "`n")
    cellVal := StrReplace(StrReplace(StrSplit(line[row], "`t")[col], " "), ",", ".")
    If cellVal is number
         Return Round(cellVal, digits)
    Else Return cellVal
}

Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: Function can not contain function, a way around?

Post by Krd » 10 Oct 2022, 00:03

Worked now by adding a sleep between Clipwait and MyVar. Weird.
Shouldn't the script wait for the function to do it's math before proceeding further? :crazy:

Post Reply

Return to “Ask for Help (v1)”