Folding the variable name - autohotkey

I want to folding tha variable name
code
numberOfInstNext := Array("","","","","","","","","","","")
numberOfInstNext[1] := 777
i := 1
I tried
msgbox %numberOfInstNext[i]%
error:
The following variable name contains an illegal character 'numberOfInstNext[i]'

Expressions says
An expression can be used in a parameter that does not directly
support it by preceding the expression with a percent sign and a space or tab.
This technique is often used to access arrays.
msgbox % numberOfInstNext[i]

I find the easiest and most scalable solution is to just assign a tmp variable each time you want to access an indexed array.
tmp := numberOfInstNext[i]
msgbox the value is %tmp%!

Related

Autohotkey IniRead pass section name as variable in AHK

I have the following tiny .ini file:
["General"]
"runs"="3"
When using this code to read the file, I get a message box stating the correct number of runs
path := A_ScriptDir "\setup.ini"
IniRead, temp, % path, "General", "runs"
MsgBox, % temp
When I try to pass General as a variable, the message box outputs "ERROR"
path := A_ScriptDir "\setup.ini"
iniSection := "General"
IniRead, temp, % path, % iniSection, "runs"
MsgBox, % temp
I tried enclosing the variables in %-signs, instead of just prefixing them, but it does not make a difference.
Any help is appreciated!
Variable names in an expression are not enclosed in percent
signs. Consequently, literal strings must be enclosed in double
quotes to distinguish them from variables.
To include an actual quote-character inside a literal string, specify
two consecutive quotes:
iniSection := """General"""

How can AutoHotkey instance variables be used in WinTitle parameters?

Let's say I have a function to get the window title of a process using its HWND id:
GetWindowTitle(hwnd) {
WinGetTitle, title, ahk_id %hwnd%
return title
}
So far, so good. It works like a charm. So, let's say I am trying to wrap some functionality within a class, in this fashion:
class RunningProcess {
hwnd := ""
windowTitle := ""
__New(hwnd) {
this.hwnd := hwnd
this.windowTitle := GetWindowTitle()
}
GetWindowTitle() {
WinGetTitle, title, ahk_id %this.hwnd%
return title
}
}
The aforementioned code fails to load with the following message:
The following variable name contains an illegal character: "this.hwnd".
I have tried several alternatives in order to use the instance variable hwnd as a WinTitle argument in WinGetTitle, to no avail. The only one that works for me involves using a local variable which grabs the contents of this.hwnd and then using the local variable as a WinTitle argument, like this:
GetWindowTitle() {
foo := this.hwnd
WinGetTitle, title, ahk_id %foo%
return title
}
However, this is less than ideal, as you might guess, at least in my opinion.
Is there a way to use this.hwnd as a WinTitle argument right off the bat?
Thanks!
I'd call it a classic case of having trouble with the legacy vs modern expression syntax.
Refreshing to see it this way though, usually people with the issue are writing 2007 level AHK, but nice to see you're writing modern AHK.
So anyway, forget about using %% to refer to variables. That's the legacy AHK way. In a modern expression statement you just type the name of the variable to refer to is, for example your assignments with the := operator are just fine that way, because it's using the modern expression syntax (= to assign would be the legacy way)
However, pretty much all commands, that haven't been replaced with more modern function, still use the legacy syntax on every parameter (unless otherwise specified in the documentation).
So, you're going to have to set the parameter to evaluate an expression, instead of expecting a legacy text parameter. To do that, you start off the parameter with a single % followed up with a space.
So your WinGetTitle command should look like this:
WinGetTitle, title, % "ahk_id " this.hwnd
Quotation marks around ahk_id , because that's how you specify a string in an expression, and then just type in the variable you want to concatenate to that string. The concatenation operator . could be used here like so % "ahk_id " . this.hwnd, but it's totally redundant and looks weird in my opinion as well.
Also, your call to the GetWindowTitle() function in the initializer __New is going to need a this. in front of it.
And initializing the instance variables to nothing is redundant as well. You can remove that if you want.
Finished product:
MyCoolObject := new RunningProcess(WinExist("A"))
MsgBox, % "The handle of currently active window is: " MyCoolObject.hwnd "`nAnd its title is: " MyCoolObject.windowTitle
class RunningProcess
{
__New(hwnd)
{
this.hwnd := hwnd
this.windowTitle := this.GetWindowTitle()
}
GetWindowTitle()
{
WinGetTitle, title, % "ahk_id " this.hwnd
return title
}
}
To learn more about legacy syntax vs expression syntax, see for example this page from the documentation:
https://www.autohotkey.com/docs/Language.htm

SPSS Macro - Generate dynamic Varnames

I am currently trying to create dynamic variable names based on the valuelabels of the passed Argument. Currently, I have something like this:
COMPUTE counter = 0.
APPLY DICTIONARY FROM *
/SOURCE VARIABLES = V601
/TARGET VARIABLES = counter.
DEFINE !macro1 (!POS !CMDEND).
STRING name (A20).
!DO !#i = 1 !TO 62
COMPUTE counter = #i
!IF (!POS !EQ !i)
!THEN
COMPUTE name = VALUELABEL(!POS)
COMPUTE !CONCAT('wasnot', name) = 1.
!ELSE
COMPUTE name = VALUELABEL(!counter).
COMPUTE !CONCAT('wasnot', name) = 0.
!IFEND
!DOEND
CROSSTABS v15 by !CONCAT('wasnot', name) /cells = column.
!ENDDEFINE.
The idea is, that for every unique value of V601 a flag variable will be created (e.g. "wasnotvaluelabel1"). This variable will either have value = 1 or 0 respectively. However, it seems that concat cannot be used the way I intended. I get these errors:
Error # 6843 in column 7. Text: !POS
The end of a macro expression occurred when an operand was expected.
Execution of this command stops.
Error # 6846 in column 7. Text: !POS
A macro expression includes an undefined macro variable or a macro operator
which is not valid within an expression.
Error # 6836 in column 12. Text: !EQ
In a macro expression, an operator was not preceded by an operand.
Error # 6846 in column 2. Text: !THEN
A macro expression includes an undefined macro variable or a macro operator
which is not valid within an expression.
Error # 6846 in column 28. Text: !POS
A macro expression includes an undefined macro variable or a macro operator
which is not valid within an expression.
Questions I have right now:
Is it even possible to generate dynamic names? I have tried
different attempts over the last hours but the SPSS macro "language"
seems very restricted.
Is there perhaps some other way to achieve this Task? It seems rather unconvenient.
Please note, working with the Python AddIn is sadly not an Option. I'm grateful for any received advice.
There is an extension command, SPSSINC CREATE DUMMIES, that will create all these dummy variables automatically. It's on the Transform menu. And it is implemented in Python.
Using Python you can easily read case data and do lots more.
Thanks for all the Help. In the end I did it with generating new syntax using Outfile.

How to save the command history in a text file in MATLAB

I want to save the value of a variable from MATLAB command history in a text. I am trying the command:
Save([d:/work/abc.txt], 'z1', '-ASCII');
An error appears
Error: input charecter is not valid in MATLAB environment or expression.
What you are missing are the quotes within the brackets for denoting string.
['string']
You should use save (with lower case for "s").
Also the filename should be defined as a string: enclose it withi two '; also you do not need the [] unless, for example, you want to build a string using a variable and / or any function to create part of the filename (e. g.
['d:/work/abc_' num2str(k) '.txt']
assuming k value is 3) to get d:/work/abc_3.txt
Try change your code to:
save(['d:/work/abc.txt'], 'z1', '-ASCII');
Hope this helps.
Qapla

check file existance in progress 4GL

How to check existance of particular file by use of code.
Eg.
def var a as character.
a = "abc.p"
run value(a).
---> here first i want to check if abc.p exist in workspace or not.
You can use the SEARCH function. Directly from the online manual:
SEARCH function
Searches the directories and libraries defined in the PROPATH environment variable for a file. The SEARCH function returns the full pathname of the file unless it is found in your current working directory. If SEARCH does not find the file, it returns the Unknown value (?).
Syntax
SEARCH ( opsys-file )
opsys-file
A character expression whose value is the name of the file you want to find. The name can include a complete or partial directory path. If opsys-file is a constant string, you must enclose it in quotation marks (" "). The value of opsys-file must be no more than 255 characters long.
Example:
DEFINE VARIABLE cPgm AS CHARACTER NO-UNDO.
cPgm = "test.p".
IF SEARCH(cPgm) <> ? THEN
RUN VALUE(cPgm).
If you provide a fully qualified pathname, SEARCH checks if the file exists. In this case, SEARCH does not search directories on the PROPATH.
If you do not want to use the propath you can use the FILE-INFO system handle.
After setting FILE-NAME, you can check the FILE-TYPE if it exists. See also the Progress Help for FILE-INFO.
FILE-INFO:FILE-NAME = a.
IF FILE-INFO:FILE-TYPE MATCHES "*F*"
THEN RUN VALUE(FILE-INFO:FULL-PATHNAME).