Help Understanding what section of code does

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Rishu0182
Posts: 33
Joined: 02 Feb 2017, 11:49

Help Understanding what section of code does

21 Sep 2018, 11:22

Hello!
So a while back someone was nice enough to help me work out some code

Code: Select all

Path := "C:\Users\User1\Desktop\letter.docx"
oWord := ComObjCreate("Word.Application")
oWord.Documents.Open(Path)
oWord.Visible := 1 
oWord.Selection.Find.ClearFormatting
oWord.Selection.Find.Replacement.ClearFormatting
for k, v in ["[Address]", Street, "[City]", City, "[State]", State, "[ZipCode]", ZipCode]
    if Mod(k, 2)
        st := v
    else
        oWord.Selection.Find.Execute(st, 0, 0, 0, 0, 0, 1, 1, 0, v, 2)
oWord := ""
however I was super busy at the time and just rolled with it. Now that I have more time, I am hoping the understand more about

Code: Select all

    if Mod(k, 2)
        st := v
Can anyone assist me in understanding this shorthand? Thanks!
awel20
Posts: 211
Joined: 19 Mar 2018, 14:09

Re: Help Understanding what section of code does

21 Sep 2018, 11:55

Code: Select all

Loop, 10
    MsgBox % Mod(A_Index, 2)
Rishu0182
Posts: 33
Joined: 02 Feb 2017, 11:49

Re: Help Understanding what section of code does

21 Sep 2018, 13:02

Ok, so I am looping through what I gather to be Boolean values being assigned to each variable in the array? So is this checking each variable to determine if true and if its true assigning its value to the variable st? If so, what is st short for? if anything
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Help Understanding what section of code does

21 Sep 2018, 13:05

Mod
Returns the remainder when Dividend is divided by Divisor.

Value := Mod(Dividend, Divisor)
S
The sign of the result is always the same as the sign of the first parameter. If either input is a floating point number, the result is also a floating point number. If the second parameter is zero, the function yields a blank result (empty string).

MsgBox, % Mod(7.5, 2) ; Returns 1.5 (2 x 3 + 1.5)
This is taken from https://autohotkey.com/docs/commands/Math.htm#Mod
K in your case is the index number. And when it reaches a specific index then st will become the value in v
awel20
Posts: 211
Joined: 19 Mar 2018, 14:09

Re: Help Understanding what section of code does

21 Sep 2018, 13:17

I'm just guessing about what "st" means, but in the function you posted in the other thread, it looks like "st" meant "search text" and "rt" meant replacement text.
Rishu0182 wrote:

Code: Select all

for k, v in ["[Address]", Street, "[City]", City, "[State]", State, "[ZipCode]", ZipCode]
    if Mod(k, 2)
        st := v
    else
        oWord.Selection.Find.Execute(st, 0, 0, 0, 0, 0, 1, 1, 0, v, 2)
The first loop iteration stores "[Address]" in "st".
The second iteration searches with "st" as the search text and Street (v) as the replacement text.
The third loop iteration stores "[City]" in "st".
The fourth loop iteration searches with "st" as the search text and City (v) as the replacement text...

Code: Select all

a := ["a", "b", "c", "d", "e", "f"]
for key, value in a
    if Mod(key, 2)
        st := value
    else
        MsgBox % key . "`n" . st . "`n" . value
Rishu0182
Posts: 33
Joined: 02 Feb 2017, 11:49

Re: Help Understanding what section of code does

21 Sep 2018, 13:20

Thank you Manny,

I did see that section in the docs about mob. So I guess where I am losing you is that st will become the value in v. Is the "st" just arbitrary? or does the "st" mean something specific?
Rishu0182
Posts: 33
Joined: 02 Feb 2017, 11:49

Re: Help Understanding what section of code does

21 Sep 2018, 13:25

Ok, so the st really is just arbitrary, it could have been "banana" and still function the same.

Thank you very much Manny and awel20 . You have both helped me greatly, and I appreciate you.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, CrowexBR, Google [Bot], Rohwedder and 200 guests