0

I am trying to extract array info using a variable, it just comes up blank though.

If I hard code it, it will display the correct info.

enter image description here

ProfileList := {}

ProfileList.Insert("Dave", {Name:"Dave",Password:"Daves Password",Server:"Regina Server"})
ProfileList.Insert("Jim", {Name:"Jim",Password:"Jims Password",Server:"Saskatoon Server"})
em :=Dave
MsgBox, % "Here is the Password for Dave: " ProfileList[%em%].Password
     . "`nHere is the Password for Jim: " ProfileList["Jim"].Password

2 Answers 2

2

You seem to be confusing legacy and expression syntax.

First lets take a look at where you try to define the variable em.
Looks like you're trying to store the string Dave in there.
If you were using legacy syntax em = Dave, you'd use the = operator to assign text to the variable, and the variable em would indeed hold the string Dave.
But you're using expression syntax em := Dave (as you should, it's not the first decade of the 2000s anymore). So you're assigning an expression to the variable em. And the expression you're assigning is Dave. Dave, as an expression, is expected to be a variable. So you're assigning the variable Dave to em. Such a variable doesn't exist though, so you're assigning nothing (empty) to em.
To assign a string to em in expression syntax, you want to do em := "Dave".

And then to the second problem, again using legacy syntax in an expression.
ProfileList[%em%].Password
%variable% would be the legacy syntax way reference a variable, but since we are in expression, we want to just do ProfileList[em].Password to reference the variable.

Legacy syntax vs expression syntax can be confusing. This page from the docs may help you out a bit:
https://www.autohotkey.com/docs/Language.htm

I would recommend trying to get into the habit of never using legacy syntax. Of course it'll work as well, but it's good practice to not use it. Maybe you'll one day want to be writing in AHK v2 and then there's no using legacy syntax.

Sign up to request clarification or add additional context in comments.

3 Comments

Ah, thanks, I was testing it out. But now that I have read your answer, I'm not sure,. I would be using %A_UserName%
What are you unsure about? A_UserName is a variable pretty much like any other one.
Oh ya, just thought I needed the % symbols MsgBox, % "Here is the Password for Dave: " ProfileList[A_UserName].Password works, thanks
1

Use quotes around "Dave" and no percent-symbols. This works:

ProfileList := {}

ProfileList.Insert("Dave", {Name:"Dave",Password:"Daves Password",Server:"Regina Server"})
ProfileList.Insert("Jim", {Name:"Jim",Password:"Jims Password",Server:"Saskatoon Server"})
em :="Dave"
MsgBox, % "Here is the Password for Dave: " ProfileList[em].Password
     . "`nHere is the Password for Jim: " ProfileList["Jim"].Password

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.