object removeAt Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
noname
Posts: 515
Joined: 19 Nov 2013, 09:15

object removeAt

21 Nov 2017, 10:06

Image

To filter non existing files i used two different methods but i cannot find why one gives a wrong result (4 files ).
Is it allowed to store the result of the filtering in the same object name ?
m:=exist_mp3(m)

Code: Select all


setworkingdir,%A_ScriptDir%
loop 6
filedelete ,file%A_Index%.mp3
sleep 1000
; create 6 files for testing
loop 6
FileAppend, ldmdmd ,file%A_Index%.mp3

m:=get_mp3(A_ScriptDir)  

; remove 3 files for testing
loop 3
FileDelete, file%A_Index%.mp3


notexist_mp3(m)

for k,v in m
list .=k "   " v  "`n"
msgbox notexist`n%list%

m:=exist_mp3(m)
list:=""
for k,v in m
list .=k "   " v  "`n"
msgbox exist`n%list%



notexist_mp3(z){
loop, % z.length()
    if !FileExist(z[A_Index])
    z.removeat(A_Index)
}

exist_mp3(z){
x:={}
loop, % z.length()
    if FileExist(z[A_Index])
    x.push(z[A_Index])
return x
}


get_mp3(dir){
z:={}
Loop, %dir%\*.mp3
  z.push(A_LoopFileFullPath)
return z
}
User avatar
Capn Odin
Posts: 1352
Joined: 23 Feb 2016, 19:45
Location: Denmark
Contact:

Re: object removeAt  Topic is solved

21 Nov 2017, 10:28

RemoveAt shifts the indexes which makes your loop miss certain indexes.

Code: Select all

lst := StrSplit("abcdefghijklm")
bool := True

loop % lst.Length() {
	MsgBox, % lst[A_Index]
	if(bool := !bool) {
		lst.removeat(A_Index)
	}
}
Notice how at the end the message box is blank because it skipped some elements.

To solve this you will need to use your own index variable.

Code: Select all

lst := StrSplit("abcdefghijklm")
bool := True
i := 1
loop % lst.Length() {
	MsgBox, % lst[i]
	if(bool := !bool) {
		lst.removeat(i)
	} else {
		i++
	}
}
Please excuse my spelling I am dyslexic.
User avatar
noname
Posts: 515
Joined: 19 Nov 2013, 09:15

Re: object removeAt

21 Nov 2017, 10:40

Thank you ,Capn Odin ,i would never have thought about that!

Can you take a look at this solution , using m to store the result is this allowed or could it produce side effects?

Code: Select all

m:=exist_mp3(m)

exist_mp3(z){
x:={}
loop, % z.length()
    if FileExist(z[A_Index])
    x.push(z[A_Index])
return x
}
Thanks in advance.
User avatar
Capn Odin
Posts: 1352
Joined: 23 Feb 2016, 19:45
Location: Denmark
Contact:

Re: object removeAt

21 Nov 2017, 10:42

It is fine to overwrite a variable with the result of a function on the same variable.
Please excuse my spelling I am dyslexic.
User avatar
noname
Posts: 515
Joined: 19 Nov 2013, 09:15

Re: object removeAt

21 Nov 2017, 11:05

Thanks :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AHK_user and 247 guests