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
Delete value in array after use question
Re: Delete value in array after use question
Not tested:
HTH
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
Re: Delete value in array after use question
Thanks Xtra 0/ I'll give this a go and let you know my results .. again thank you for replying which such a great answerXtra wrote:Not tested:HTHCode: 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
Re: Delete value in array after use question
I'm having a problem working out what this part of the code is
counter++
I dont understand what it is ?
counter++
I dont understand what it is ?
Re: Delete value in array after use question
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
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
Re: Delete value in array after use question
Hi Xtra , is it a method or function or is it a variable ?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
Re: Delete value in array after use question
It is a variable.
Re: Delete value in array after use question
++ 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
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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Who is online
Users browsing this forum: peter_ahk, Spawnova, toddhere, USS_Sandhu and 315 guests