A problem with nested for-loop

Get help with using AutoHotkey and its commands and hotkeys
john_c
Posts: 340
Joined: 05 May 2017, 13:19

A problem with nested for-loop

18 Sep 2019, 14:30

I have one for-loop inside another one.

Both of them have identical key-value names: Index, File.

I understand that identical names are, probably, not a good idea, but I don't want to change them. (For a number of reasons. Actually, it is a part of quite large and complicated script.)

The further explanations are better to show as part of the code itself:

Code: Select all

ModificationTimeOld := {}
ModificationTimeNew := {}

Files1 := ["foo.txt", "bar.txt", "baz.txt"]
Files2 := ["foo.png", "bar.png", "baz.png"]

for Index, File in Files1
{
    FileGetTime, ModificationTimeNew, % File, M
    ModificationTimeNew[File] := ModificationTimeNew
    if (ModificationTimeOld[File] != ModificationTimeNew[File])
    {
        ; This line updates the "last modification" date of the files
        ; inside the Files1 array.
        ;
        ; It works when located above the inner loop. However, due to
        ; the inner logic of my script, I want to move it below.
        ; The problem is, that when I move it below, it starts to "read"
        ; the modification dates of the inner loop, instead of outer.
        ; Is there a way to fix it?
        ModificationTimeOld[File] := ModificationTimeNew[File]

        for Index, File in Files2
        {
            ; Of course, in real life here goes another things.
            SoundBeep, 300, 300
        }

        ; This is the "target" place.
        ; I.e. I want "ModificationTimeOld[File] := ModificationTimeNew[File]"
        ; line to be located here.
    }
}
User avatar
Capn Odin
Posts: 1308
Joined: 23 Feb 2016, 19:45
Location: Denmark

Re: A problem with nested for-loop

18 Sep 2019, 14:34

Move the inner loop to a function or use different names. Alternatively you could save the variables to a temporary one and restore it after the inner loop.

Code: Select all

ModificationTimeOld := {}
ModificationTimeNew := {}

Files1 := ["foo.txt", "bar.txt", "baz.txt"]
Files2 := ["foo.png", "bar.png", "baz.png"]

for Index, File in Files1 {
	FileGetTime, ModificationTimeNew, % File, M
	ModificationTimeNew[File] := ModificationTimeNew
	if(ModificationTimeOld[File] != ModificationTimeNew[File]) {
		ModificationTimeOld[File] := ModificationTimeNew[File]
		doSomethingWithFiles(Files2)
	}
	MsgBox, % Index ": " File
}

doSomethingWithFiles(Files) {
	for Index, File in Files {
		SoundBeep, 300, 300
	}
}
Please excuse my spelling I am dyslexic.
john_c
Posts: 340
Joined: 05 May 2017, 13:19

Re: A problem with nested for-loop

18 Sep 2019, 15:23

> Move the inner loop to a function or use different names. Alternatively you could save the variables to a temporary one and restore it after the inner loop.

The problem is that all these "ways" (except changing the names) makes the code hard-to-understand. This is the same problem that I have currently.

Actually, I hope to find something like

Code: Select all

ModificationTimeOld[Files1.File] := ModificationTimeNew[Files1.File]
hd0202
Posts: 144
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: A problem with nested for-loop

20 Sep 2019, 01:00

here is my suggestion for your script, please have a look at my comments inside, one is very important:

Code: Select all

ModificationTimeOld := {}
ModificationTimeNew := {}

Files1 := ["foo.txt", "bar.txt", "baz.txt"]
Files2 := ["foo.png", "bar.png", "baz.png"]

for Index, File in Files1
{
    FileGetTime, TimeNew, % File, M	; do not use ModificationTimeNew, it destroys the array !
    ModificationTimeNew["Files1", File] := TimeNew	; use new variable
    if (ModificationTimeOld["Files1", File] != ModificationTimeNew["Files1", File])
    {
;       ModificationTimeOld["Files1", File] := ModificationTimeNew["Files1", File]

		loop_files2(files2)	; and other needed variables as parameter

        ModificationTimeOld["Files1", File] := ModificationTimeNew["Files1", File]
    }
}
exitapp

loop_files2(files2)	; and other needed variables as parameter
{
        for Index, File in Files2
        {
            ; Of course, in real life here goes another things.
            SoundBeep, 300, 300
        }
}
Hubert

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], FreakyDoctor, Xtra and 172 guests