Problem passing list of window ids to function

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
bobf32
Posts: 4
Joined: 16 Feb 2017, 05:24

Problem passing list of window ids to function

16 Feb 2017, 05:32

Hi,

Massive noob question: why does this work:

Code: Select all

WinGet, id, list,/cygdrive/h,, Program Manager
fSendDev1ToWindows(id)

fSendDev1ToWindows(id) {
    Loop, %id%
    {
        MsgBox %A_Index%
        this_id := id%A_Index%
        MsgBox %this_id%
        WinActivate, ahk_id %this_id%
;        Send, dev2{Enter}
    }

    RETURN
}
and this not?

Code: Select all

WinGet, id, list,/cygdrive/h,, Program Manager
fSendDev1ToWindows(id)

fSendDev1ToWindows(p_id) {
    Loop, %p_id%
    {
        MsgBox %A_Index%
        this_id := p_id%A_Index%
        MsgBox %this_id%
        WinActivate, ahk_id %this_id%
;        Send, dev2{Enter}
    }

    RETURN
}
Specifically, in the latter call the variable this_id does not appear to get populated.
kanat
Posts: 26
Joined: 22 Oct 2013, 01:35

Re: Problem passing list of window ids to function

16 Feb 2017, 07:34

Really weird behaviour. Somebody of the gurus should explain it.
User avatar
jmeneses
Posts: 524
Joined: 28 Oct 2014, 11:09
Location: Catalan Republic

Re: Problem passing list of window ids to function

16 Feb 2017, 08:56

Hello to you both look at this

Code: Select all

WinGet, id, list,,, Program Manager   
fSendDev1ToWindows1(id)
Msgbox 0x40000,, % "(ListVars of fSendDev1ToWindows1).......... " 
fSendDev1ToWindows2(id)
Msgbox 0x40000,, % "END!!!! "       
ExitApp

fSendDev1ToWindows1(id) {   
    Listvars
    RETURN
}

fSendDev1ToWindows2(p_id) {
    Listvars
    RETURN
}

See https://autohotkey.com/docs/Functions.htm#PseudoArrays
Donec Perficiam
bof32

Re: Problem passing list of window ids to function

16 Feb 2017, 09:50

Thanks for this. Unfortunately I have no idea a) what it means or b) what I need to do to get my p_id version working!
bobf32
Posts: 4
Joined: 16 Feb 2017, 05:24

Re: Problem passing list of window ids to function

16 Feb 2017, 10:03

Thanks, but I have no idea how this helps!
4GForce
Posts: 553
Joined: 25 Jan 2017, 03:18
Contact:

Re: Problem passing list of window ids to function

16 Feb 2017, 13:10

bobf32 wrote:Specifically, in the latter call the variable this_id does not appear to get populated.
Because p_id1 doesn't exist ...

In the 1st one when using id%A_Index% you're accessing the global variable

The only thing that you pass to your function is id into p_id ... that is only the number of window found.

Pseudo arrays are imaginary arrays, they are actually just a bunch of variables, that's why he was pointing you at ListVars.
bobf32
Posts: 4
Joined: 16 Feb 2017, 05:24

Re: Problem passing list of window ids to function

16 Feb 2017, 13:46

Thanks, I see. Sort of. So given what I'm trying logically to do is pass an array to a function would it be best to just use the global or is there a better way to pass these pseudo arrays?
4GForce
Posts: 553
Joined: 25 Jan 2017, 03:18
Contact:

Re: Problem passing list of window ids to function

16 Feb 2017, 14:11

bobf32 wrote:Thanks, I see. Sort of. So given what I'm trying logically to do is pass an array to a function would it be best to just use the global or is there a better way to pass these pseudo arrays?
You can't really pass them to functions and they won't be global if created inside a function.
If you keep the code structure you have now, use the global.

If you want to avoid the globals, you could do something like this ...

Code: Select all

fSendDev1ToWindows(getWinIds())

getWinIds() {
   	 winIds := []
	WinGet, id, list,,, Program Manager
   	 loop % id
    	{
        	winIds[A_Index] := id%A_Index%
    	}
	return winIds
}

fSendDev1ToWindows(Ids) {
    	For i, id in Ids
    	{
        	MsgBox % A_Index . " -> " . id
        	WinActivate, ahk_id %id%
		;  Send, dev2{Enter}
   	 }
   	 return
}
bobf32
Posts: 4
Joined: 16 Feb 2017, 05:24

Re: Problem passing list of window ids to function

16 Feb 2017, 14:36

Many thanks for all the help.
User avatar
jmeneses
Posts: 524
Joined: 28 Oct 2014, 11:09
Location: Catalan Republic

Re: Problem passing list of window ids to function

17 Feb 2017, 03:18

Thaks 4GForce for helping out in explaining my english is very++ bad :trollface:
Donec Perficiam

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ishida20, jameswrightesq, Lem2001 and 402 guests