if statement necessary here? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

if statement necessary here?

15 Dec 2019, 19:55

Hey guys,

I'm back after a long AHK pause ;)
I guess I lost some skills :think:

Anyway, in my script I call a function and depending on the value, it should output values inside of a MessageBox.

Code: Select all

F1::
A = 212
1 = 966
letter = %A%
digit = %1%
MsgBox, %letter%, %digit%
return

F2::
Click_On_Field("A1")
return

F3::
Click_On_Field_New("A1")
return

Click_On_Field(field)
{
  StringTrimLeft, digit, field, 1
  StringTrimRight, letter, field, 1
  
  if letter = A
  {
    letter = 212
  }
  if letter = B
  {
    letter = 329
  }
  if digit = 1
  {
    digit = 966
  }
  if digit = 2
  {
    digit = 855
  }
  
  MsgBox, %letter%, %digit%
}

Click_On_Field_New(field)
{
  StringTrimLeft, digit, field, 1
  StringTrimRight, letter, field, 1
  A = 212
  1 = 966
  MsgBox, %letter%, %digit%
}
The 1st function works, but is there a way that I can omit the if statement to get the same results, like I tried in the 2nd function?
The F1 and the F2 script show the correct result.
F3 only shows A and 1, but not the values which are assigned to them.

How to code the F3 script so that it shows 212 and 966 instead of A and 1?
Or is the if block really necessary?

Thanks for any help!
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: if statement necessary here?  Topic is solved

15 Dec 2019, 20:13

This?

Code: Select all

Click_On_Field(field)
{
  	StringTrimLeft, digit, field, 1
  	StringTrimRight, letter, field, 1
	letter := ( letter = "A" ) ? ( 212 ) : ( letter = "B" ) ? ( 329 ) : ( "" ) 
	digit := ( digit = 1 )?( 966 ):(digit = 2 ) ? ( 855 ) : ( "" )   
  	MsgBox, %letter%, %digit%
}


Also, i'd advise against using simple numbers as variable names.
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: if statement necessary here?

16 Dec 2019, 00:04

Code: Select all

F3::Click_On_Field("A1")

Click_On_Field(field)
{
    static fields := {A1: "212,966", A2: "212,855", B1: "329,966", B2: "329,855"}
    ;~ Click % fields[field]
    MsgBox % fields[field]
}
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: if statement necessary here?

16 Dec 2019, 00:33

Scr1pter wrote:
15 Dec 2019, 19:55
F3 only shows A and 1, but not the values which are assigned to them.

How to code the F3 script so that it shows 212 and 966 instead of A and 1?
you need a double-deref.

dereferencing %letter% only returns "A", so we need to then deref the result of that. you can think of it like %(%letter%)%. but, that syntax is invalid in AHK

here are 3 examples of working syntax:

Code: Select all

Click_On_Field_New(field)
{
  StringTrimLeft, digit, field, 1
  StringTrimRight, letter, field, 1
  A = 212
  1 = 966
  MsgBox, % %letter% . "-" . %digit%

  lettervalue := %letter%
  digitvalue := %digit%
  MsgBox, %lettervalue% - %digitvalue%

  lettervalue2 = % %letter%
  digitvalue2 = % %digit%
  MsgBox, %lettervalue2% -- %digitvalue2%
}
all of your code uses regular old style assignments with =, rather than expression assignements with :=, so probably the last example is what you want

User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: if statement necessary here?

18 Dec 2019, 20:23

Thank you, guys!

Xtra's idea is interesting, however, it would require that I'll do it for all 64 fields.
For this reason I used the A - H and 1 - 8 way.
By combining them, I'll achieve all 64 fields (8x8)

guest3456's post makes sense too, however, I chose Hellbent's code.
Works perfectly - I'll write down this method.

Cheers!
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb and 372 guests