Need to display strings instead of a hotkey label using tooltip Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Class1
Posts: 39
Joined: 28 Sep 2017, 02:51

Need to display strings instead of a hotkey label using tooltip

16 Oct 2017, 22:37

As you can see my code has 4 types of variables_name, age, hotkey and array.

My goal is to display a string instead of a hotkey label via tooltip, but I'm having a hard time doing that?

any help would be appreciated :)

Code: Select all

;Name
NameVarA := "Alex"
NameVarB := "Robert"

;Age
ageVarA = 40
ageVarB = 50

;Hotkey
VarA = O
VarB = P
Hotkey, %VarA%, VarA
Hotkey, %VarB%, VarB

;Array
age := {VarA:ageVarA, VarB:ageVarB}
index = VarA

VarA: ;GOAL: I want to display the name Alex instead of VarA
VarB: ;GOAL: I want to display the name Robert instead of VarB
index := A_ThisLabel
ToolTip(index ": " age[index])
return

;Don't pay attention
ToolTip(label){
  ToolTip, %label%, 930, 650
  SetTimer, RemoveToolTip, 1000
  return
  RemoveToolTip:
  SetTimer, RemoveToolTip, Off
  ToolTip
  Return
  }
Noesis
Posts: 301
Joined: 26 Apr 2014, 07:57

Re: Need to display strings instead of a hotkey label using tooltip  Topic is solved

16 Oct 2017, 23:44

FYI, you're using an object not an array, technically the object you've commented as an array isn't really an array as an array wouldn't have named keys (i.e strings as keys). To put it simply an array (in ahk terms) is still an object but it's an object which only contains numbered keys.

The issue is the arguments being used in calling the Tooltip function, you are essentially using A_Thislabel (assigned to index) which is only ever going to be equal to either "VarA" or "VarB", hence when you call Tooltip(index ": " age[index]) you only ever get the label name. What you probably want is nameVarA or nameVarB to be used instead so you need to change the call to be:

Code: Select all

Tooltip(name%index% ": " age[index])
Incidentally you could do exactly the same thing to get age so you don't really need the object at all, but perhaps you want it ? or perhaps you'd rather be using an object for both instead.
Class1
Posts: 39
Joined: 28 Sep 2017, 02:51

Re: Need to display strings instead of a hotkey label using tooltip

17 Oct 2017, 00:47

Noesis wrote:FYI, you're using an object not an array, technically the object you've commented as an array isn't really an array as an array wouldn't have named keys (i.e strings as keys). To put it simply an array (in ahk terms) is still an object but it's an object which only contains numbered keys.

The issue is the arguments being used in calling the Tooltip function, you are essentially using A_Thislabel (assigned to index) which is only ever going to be equal to either "VarA" or "VarB", hence when you call Tooltip(index ": " age[index]) you only ever get the label name. What you probably want is nameVarA or nameVarB to be used instead so you need to change the call to be:

Code: Select all

Tooltip(name%index% ": " age[index])
Incidentally you could do exactly the same thing to get age so you don't really need the object at all, but perhaps you want it ? or perhaps you'd rather be using an object for both instead.
wow thanks, it worked, but I was wondering what's the other way you told I can do it ?
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Need to display strings instead of a hotkey label using tooltip

17 Oct 2017, 02:38

Noesis wrote:FYI, you're using an object not an array, technically the object you've commented as an array isn't really an array as an array wouldn't have named keys (i.e strings as keys). To put it simply an array (in ahk terms) is still an object but it's an object which only contains numbered keys.
Hello Noesis :wave: , in my opinion, this,

Code: Select all

{key1:val1,key2:val1}
is an associative array, which is an object. The keys can be either integers, strings or objects, given any combination, it is still an associative array. An array which only contains integer keys is sometimes refered to as a linear array. In the following,

Code: Select all

a:=array()
b:=object()
c:=[]
d:={}
a,b,c and d all are of the same type. The way of creating the arrays above only differ in input parameters. Arrays are always objects, objects might not be arrays, they can be some of the built-in ones, eg, func or (regex) match objects.
Cheers
Noesis
Posts: 301
Joined: 26 Apr 2014, 07:57

Re: Need to display strings instead of a hotkey label using tooltip

18 Oct 2017, 00:25

Hi Helgef,

Well you are correct, but in my mind it isn't really helpful to newer users of ahk to complicate things like that. For simplicity sake I think it's easier to think in terms of Array (on it's own) to mean simple/linear array's, and if you're talking Associative Array's or Pseudo Arrays, then it's probably not a good idea to drop the "Associative" or "Pseudo" prior to array. So I suppose to me, array on it's own implies "Simple Array".

Personally I don't really use the term Associative Arrays instead I'd phrase them as objects, it just makes it clearer IMO, especially when you consider what an array is generally considered to be in other programming languages both now and traditionally. Most languages these days do have data types that can contain numerical and/or named keys but they are also usually distinguished from arrays, by calling them something else.

To be totally technical about it, ahk doesn't really have an array type at all, it has objects, some of which can act the same as an array elsewhere would act, and then there are Psuedo Arrays, which are essentially dynamically named variables, but traditionally they are AHK's arrays from prior to objects being introduced. A similar case could be made to how ahk doesn't really have classes either, they are also objects, but to be honest, I don't really care a great deal about what something is called, I'm more interested in how it works.

One thing I'm curious about is given the broad definition of Associative Arrays, and the statement towards the end,
Helgef wrote:Arrays are always objects, objects might not be arrays, they can be some of the built-in ones, eg, func or (regex) match objects.
I would agree Objects aren't always Arrays, but to me if you wan't to include "Associative Arrays" as being meant by the term Arrays, then the above statement IMO becomes false as to me All objects are also "Associative Arrays". If you disagree, can you provide an example of an object which doesn't meet the criteria of an associative array ? because I honestly can't think of any, hence my presumption of Array meaning simple Array.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Need to display strings instead of a hotkey label using tooltip

18 Oct 2017, 06:17

Noesis, thanks for you well formulated reply. Terminology isn't always well defined or agreed upon, and often depends on context. That is why I expressed my comments as opinions. I continue expressing my opinions. I think you might be over analysing my statements. To me, an array always is associative, because, there is association between keys and values. Now, if the meaning of the keys are only positional, I wouldn't explicitly name the array as an associative array. If the keys have another meaning than position, I might still just call it an array, but I would be more likely to spell out the word associative. Now, just because an object contains associations between keys and values, I wouldn't necessarily call it an (associative) array. I think it is most practical to name objects such that their purpose is best described. Eg, func objects contains key value pairs, but the word func is much more descriptive of its purpose, calling a function. Calling regex objects arrays would be unnencessarily confusing, since they are named in the documentation, and lacks most features of the usual arrays (eg, []).
One thing I'm curious about is given the broad definition of Associative Arrays, and the statement towards the end,
Helgef wrote:Arrays are always objects, objects might not be arrays, they can be some of the built-in ones, eg, func or (regex) match objects.
I would agree Objects aren't always Arrays, but to me if you wan't to include "Associative Arrays" as being meant by the term Arrays, then the above statement IMO becomes false as to me All objects are also "Associative Arrays". If you disagree, can you provide an example of an object which doesn't meet the criteria of an associative array ? because I honestly can't think of any, hence my presumption of Array meaning simple Array.
I'm not sure I understand what you mean by this, or if the above answers it.
in my mind it isn't really helpful to newer users of ahk to complicate things like that.
The reason I answered, was beacuse I thought you where making it unnecessarily complicated by stating that age := {VarA:ageVarA, VarB:ageVarB} isn't an array, but instead calling it by the completely abstract name object. To me, it is clear that sole purpose of the object age is to associate VarA with ageVarA and so on.
@ Op, sorry for going off topic :offtopic: . I would like to emphasise that there was a follow-up question to Noesis first post,
I was wondering what's the other way you told I can do it ?
Cheers. :)
Noesis
Posts: 301
Joined: 26 Apr 2014, 07:57

Re: Need to display strings instead of a hotkey label using tooltip

19 Oct 2017, 02:33

Class1 wrote: wow thanks, it worked, but I was wondering what's the other way you told I can do it ?
Ok sorry, Class1, I got a little side tracked. What I meant was there are several ways you could do this, and I'm not sure what you're actually trying to accomplish. By that I mean I'm not really sure the code itself is meant to achieve something useful, I'm more inclined to think you are trying things out and seeing what can be done. I could be completely wrong but essentially it's completely up to you, there isn't really a right or wrong way to do it, but you could use variables like you are without using an object i.e:

Code: Select all

;Name
NameVarA := "Alex"
NameVarB := "Robert"

;Age
ageVarA = 40
ageVarB = 50

;Hotkey
VarA = O
VarB = P
Hotkey, %VarA%, VarA
Hotkey, %VarB%, VarB

VarA: ;GOAL: I want to display the name Alex instead of VarA
VarB: ;GOAL: I want to display the name Robert instead of VarB
index := A_ThisLabel
ToolTip(name%index% ": " age%index%)
return
or you could use an object instead, something like:

Code: Select all

;Name
Data:={VarA:{Name:"Alex",Age:40},VarB:{Name:"Robert",Age:50}}

;Hotkey
VarA = O
VarB = P
Hotkey, %VarA%, VarA
Hotkey, %VarB%, VarB

VarA: ;GOAL: I want to display the name Alex instead of VarA
VarB: ;GOAL: I want to display the name Robert instead of VarB
index := A_ThisLabel
ToolTip(Data[index].Name ": " Data[index].age)
return
It's not really a big deal either way, It's more about whichever method you feel more comfortable with, and I suppose knowing there's more than one way you could do it. Even the object used in the second example could be different, or you could have two seperate ones for age and name it's completely up to you, main thing to note (which I think you're already aware of) is there should be a way of identifying which variable or object key to use, from the label, i.e. VarA, VarB.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: SimmoF, uchihito and 223 guests