Remove empty elements from pseudoarray?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
frexum
Posts: 7
Joined: 23 Mar 2018, 17:02

Remove empty elements from pseudoarray?

21 Nov 2018, 13:33

So I have the code below and it functions the way I want to, it's just a little slow. What I think is slowing it down is the string split into new lines creates a bunch of empty elements that all still get checked in the loop. I've tried to search and can't find anything to remove these to speed up the script. Any help would be appreciated. The purpose of the code is to just check to sets of numbers from different sources against each other and then pop up with a message of the missing elements

F5::
clipboard =
Clipsaved =
ClipNew =
sendinput,{control down}c
sleep,50
sendinput,{control up}
Clipwait
clipboard:= regexreplace(clipboard,"\D","`n")
vText := % clipboard
oArray := StrSplit(vText, "`n")
Haystack := drugset
Missed:= []
clipboard :=
Maxx := oArray.MaxIndex()
for index, element in oArray
{
Progress, %a_index%/%Maxx%,%a_index%/%Maxx%,%a_index%/%Maxx%
sleep,15
If InStr(Haystack, element)
{

}
Else
{
sleep,50
Missed.Push(element)
Clipsaved := % element
ClipNew=`n%ClipSaved%`n%clipboard%
clipboard = %ClipNew%
}
sleep,50
}
Progress,off
MsgBox,,Missed GPIs have been added to clipboard, % join(Missed)
join( strArray )
{
s := ""
for i,v in strArray
s .= ", " . v
return substr(s, 3)
}

return


I tried to do the oArray.Remove("") but that didn't do anything. Another thing I was thinking was that maybe there was a better way to create the array to begin with?

Thank you for looking
User avatar
jackdunning
Posts: 126
Joined: 01 Aug 2016, 18:17
Contact:

Re: Remove empty elements from pseudoarray?

21 Nov 2018, 14:22

I could be wrong, but it seems that all the Sleep commands could be causing your script to slow down much more than the StrSplit() function. I don't see empty array elements alone slowing things down very much.

However, you could also use the Continue command to skip empty array elements in the For-loop.
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Remove empty elements from pseudoarray?

21 Nov 2018, 14:25

remove all Sleep commands, what are they there for?

and this doesn't look right

Code: Select all

	If InStr(Haystack, element)
	{

	}
	Else
User avatar
bitx0r
Posts: 21
Joined: 05 Oct 2014, 12:30
Location: NorCal
Contact:

Re: Remove empty elements from pseudoarray?

21 Nov 2018, 15:24

MThis doesn't look right either:

Code: Select all

join(strArray) 
{
        s := ""
        for i,v in strArray
                s .= ", " . v
        return substr(s, 3)
}
        

return
I believe that Functions should declared outside of SubRoutines... or maybe I've been doing this AHK thing all wrong. I also noticed some Assignments in Expression form := with a Forced Expression % which isn't needed with :=

Try this:

Code: Select all

F5::
    clipboard := Clipsaved := ClipNew := ""
    
    sendinput,{control down}c
    sleep,50
    sendinput,{control up}
    Clipwait
    clipboard := regexreplace(clipboard,"\D","`n")
    vText := clipboard
    oArray := StrSplit(vText, "`n")
    Haystack := drugset
    Missed:= []
    clipboard :=
    Maxx := oArray.MaxIndex()
    for index, element in oArray {
        Progress, %a_index%/%Maxx%,%a_index%/%Maxx%,%a_index%/%Maxx%
        sleep,15
        If InStr(Haystack, element)
            Continue
        Else {
            sleep,50
            Missed.Push(element)
            Clipsaved := element
            ClipNew=`n%ClipSaved%`n%clipboard%
            clipboard = %ClipNew%
        }    
    sleep,50
    }
    
    Progress,off
    MsgBox,,Missed GPIs have been added to clipboard, % join(Missed)
    
return

join(strArray) {
    s := ""
    for i,v in strArray
            s .= ", " . v
    return substr(s, 3)
}
awel20
Posts: 211
Joined: 19 Mar 2018, 14:09

Re: Remove empty elements from pseudoarray?

21 Nov 2018, 15:34

frexum wrote:
21 Nov 2018, 13:33

Code: Select all

ClipNew=`n%ClipSaved%`n%clipboard%
clipboard = %ClipNew%
This will read and write to the clipboard for every missing element. The clipboard is a lot slower than a regular variable.

It looks like you just want to find the lines that do not contain the number stored in 'drugset'. Perhaps you could just use RegExReplace to remove the undesired lines? (Instead of looping)
Edit

Code: Select all

Progress, %a_index%/%Maxx%,%a_index%/%Maxx%,%a_index%/%Maxx%
Updating the progress bar for every element may also be slowing you down. Updating that less often should speed things up a bit.

Code: Select all

If InStr(Haystack, element)
{

}
Else
{...
not related to speed... and A.B. already mentioned it, but you can do this instead:

Code: Select all

If !(InStr(Haystack, element)) ; if not in string
{
...
}
frexum
Posts: 7
Joined: 23 Mar 2018, 17:02

Re: Remove empty elements from pseudoarray?

21 Nov 2018, 17:34

Thank you everyone for all your comments. The adding sleep every few lines was probably a bad habit from starting programming. Removing all of those sped the code up a TON! I will work on your other suggestions. Thanks again!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Billykid, Google [Bot], haomingchen1998, mikeyww and 266 guests