Delete value in array after use question

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
chango
Posts: 30
Joined: 27 May 2017, 12:10

Delete value in array after use question

18 Mar 2018, 15:04

Hi folks, I have a very simple piece of code that opens a text file writes the contents line by line into an array.

Read_file:
emails := ""
Loop, Read, C:\Users\Administrator\Documents\text_text.txt
{
emails .= A_loopReadLineA_Space
}
EmailsArray := []
Loop, Parse, emails, %A_Space%
{
EmailsArray.Push(A_loopField)
}
For key, value in EmailsArray
{
MsgBox, 0, , %Value%
}
Return

What i want to do is use each Value (line) for output, and then delete the value (line) from the array.
The idea is to use lines once until there are no more, then re loop the above function to repopulate the array and start again.
The reason behind this is .. if I have to exit the script I don't want the script to start from the first value (line) when restarted as that would create duplicates of submissions. :)
Thanks
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Delete value in array after use question

18 Mar 2018, 16:02

Not tested:

Code: Select all

#NoEnv
OnExit, saveDataAndExit    ; auto save

fileName := "C:\Users\Administrator\Documents\text_text.txt"    ; Original file
tempName := "C:\Users\Administrator\Documents\text_text.tmp"    ; Temp file

Read_file:
emails := EmailsArray := ""    ; Clear

if FileExist(tempName)
{
    FileRead, emails, % tempName
    FileDelete, % tempName    ; Delete temp file
}
else if FileExist(fileName)
    FileRead, emails, % fileName
else
    MsgBox, 4112, Fileread Error!, File does not exist`n%fileName%

EmailsArray := StrSplit(emails,"`n","`r")    ; Put each line into array

For key, value in EmailsArray
{

    ; <<< process value here <<<

    EmailsArray.Delete(Key)    ; Remove value from array after it is processed
}

return

saveDataAndExit:
For key, value in EmailsArray
{
    if (value != "")    ; Some values will be empty from being processed so check here
    {
        counter++
        save .= value . "`n"
    }
}
if (counter)
    FileAppend, % SubStr(save,1,-1), % tempName    ; Remove trailing `n and save file
ExitApp
HTH
chango
Posts: 30
Joined: 27 May 2017, 12:10

Re: Delete value in array after use question

18 Mar 2018, 16:47

Xtra wrote:Not tested:

Code: Select all

#NoEnv
OnExit, saveDataAndExit    ; auto save

fileName := "C:\Users\Administrator\Documents\text_text.txt"    ; Original file
tempName := "C:\Users\Administrator\Documents\text_text.tmp"    ; Temp file

Read_file:
emails := EmailsArray := ""    ; Clear

if FileExist(tempName)
{
    FileRead, emails, % tempName
    FileDelete, % tempName    ; Delete temp file
}
else if FileExist(fileName)
    FileRead, emails, % fileName
else
    MsgBox, 4112, Fileread Error!, File does not exist`n%fileName%

EmailsArray := StrSplit(emails,"`n","`r")    ; Put each line into array

For key, value in EmailsArray
{

    ; <<< process value here <<<

    EmailsArray.Delete(Key)    ; Remove value from array after it is processed
}

return

saveDataAndExit:
For key, value in EmailsArray
{
    if (value != "")    ; Some values will be empty from being processed so check here
    {
        counter++
        save .= value . "`n"
    }
}
if (counter)
    FileAppend, % SubStr(save,1,-1), % tempName    ; Remove trailing `n and save file
ExitApp
HTH
Thanks Xtra 0/ I'll give this a go and let you know my results .. again thank you for replying which such a great answer :)
chango
Posts: 30
Joined: 27 May 2017, 12:10

Re: Delete value in array after use question

04 Apr 2018, 10:59

I'm having a problem working out what this part of the code is

counter++

I dont understand what it is ?
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Delete value in array after use question

04 Apr 2018, 11:06

counter++ increments the counting of the number of non-empty values we are going to write to the file.
If there are no values it does not count them.
If there is nothing counted it skips writing to file which would be a blank line.

HTH
chango
Posts: 30
Joined: 27 May 2017, 12:10

Re: Delete value in array after use question

04 Apr 2018, 13:08

Xtra wrote:counter++ increments the counting of the number of non-empty values we are going to write to the file.
If there are no values it does not count them.
If there is nothing counted it skips writing to file which would be a blank line.

HTH
Hi Xtra , is it a method or function or is it a variable ?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Delete value in array after use question

04 Apr 2018, 14:35

++ is an operator.

Links:
Variables and Expressions
https://autohotkey.com/docs/Variables.htm#Operators
Midnight Star - "Operator" (Official Video) - YouTube
https://www.youtube.com/watch?v=hTFu1ns-fJ4
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 110 guests