0

Been stuck for a while, hoping someone with experience could help me a bit...

Right now I'm reading Ini like this three times but wish to do it dynamically to improve my codes in the long run.

IniRead, Alert_price_low, %TrackerFile%, Item, Alert_price_low
IniRead, Alert_price_high, %TrackerFile%, Item, Alert_price_high
IniRead, Alert_checkbox, %TrackerFile%, Item, Alert_checkbox

I made this function below trying to read it dynamically, but doesn't return anything...

FindIniSettings(TrackerFile, Alert_checkbox)

FindIniSettings(TrackerFile, x){
    x := IniRead, %x%, TrackerFile, Item, %x%
    return x
}

How my Ini file content looks like:

[Item]
Alert_price_low=777
Alert_price_high=999
Alert_checkbox=1

Thanks!

DagicCross
  • 401
  • 3
  • 13

1 Answers1

1

The problem here is pretty much just with the usage of the new expression syntax.
Seems you've been using only the deprecated legacy syntax, and functions are not apart of that.

So first problem is here
FindIniSettings(TrackerFile, Alert_checkbox)
Here you're not in legacy syntax, so to specify strings you quote them.
FindIniSettings(TrackerFile, "Alert_checkbox")
(And I'm assuming TrackerFile here is a variable that contains some iniFileNameOrPath.ini string)
Also, you're not storing the return value of this function anywhere.

Second problem is here
x := IniRead, %x%, TrackerFile, Item, %x%
Firstly, commands are legacy, they don't return values like that.
You can't use an := operator to get the return value.
They return values only by writing the output to the requested variable, which will be specified in the first parameter of the command.
You specified the output variable to be named whatever x contains. This is no good since you can't possibly know what the output variable is going to be during runtime (without some unnecessary extra tricks).
Also, be quite confusing to name the output to be the same as input key. Though, that would work.

So two problems there, := and the first %x%, and there's still some more to go which is right here:
, TrackerFile,
Commands are legacy, as mentioned above, they exclusively use the legacy syntax in every parameter (unless otherwise specified in the documentation).
So, you're passing in the literal text "TrackerFile", not whatever string should be stored inside the variable named TrackerFile.
In legacy syntax, you refer to the contents of a variable by wrapping it around in %%, as you've been doing before. Maybe you just forgot here.
But really, I'd recommend you try to get used to ditching the legacy syntax. So what you could/should do, is starting off the parameter with a single % followed up by a space. This makes ahk interpret an expression on that parameter, as opposed to using the legacy syntax. In the modern expression syntax you refer to a variable just by typing its name. No stupid %%s needed.


So here's the fixed script you should end up with. I made this example fully legacy syntax free as a demonstration:

TrackerFile := "test.ini"

returnValueOfThisFunction := FindIniSettings(TrackerFile, "Alert_price_low")
MsgBox, % "Yay, the function returned a value for us:`n" returnValueOfThisFunction

return


FindIniSettings(TrackerFile, key)
{
    ;even specified the string "Item" explicitly as a string
    ;this is not needed, but it just looks right to me
    ;I wouldn't want to specify a legacy parameter there
    ;in the middle of all this
    IniRead, output, % TrackerFile, % "Item", % key
    MsgBox, % "Now the variable named ""output"" contains the result of the above command (" output ").`nWe can now return it to the caller."
    return output
}

So yeah, pretty much just problems understanding legacy syntax vs expression syntax.
You can give a read to a previous answer of mine here about usage of %% vs % .
And here's a good page on the AHK documentation about the scripting language and legacy vs. modern expression.

0x464e
  • 5,948
  • 1
  • 12
  • 17