AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How can I reset variables

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Uminnsky



Joined: 12 Aug 2009
Posts: 26

PostPosted: Sun Aug 23, 2009 12:33 am    Post subject: How can I reset variables Reply with quote

Simple problem...for the life of me I cannot debug my script and fix it, but basically I have a script that reads from a large txt file, performs a lot of manipulations to it, and then ultimately spits out the formatted reults in a new text file.

I have my program set up to operate on a given text file based on user input. The problem I am encountering is that as I switch between files being worked on a remnant of the previous file is appended to the new file loaded.

If I close my script and reopen it the script will load the new file correctly, so somewhere in my script something is not resetting between executing the code. Is there any way to reset my vairables without reloading the entire GUI and there by losing the user input?

I am new so the code represents my lack of understanding from the start, but, essentially, by time it gets to fileappend...test.txt, the temporary file appends a remnant from the previous time it was run (assuming this was the second time the script was excuted).

Thanks!


Code:
ButtonViewPicks:
filedelete, %A_ProgramFiles%\tracks\Test.txt
FileDelete,%programfiles%\tracks\%trk%.txt
FileDelete, %A_ProgramFiles%\tracks\picks.txt
FileDelete, %A_ProgramFiles%\tracks\raceone.txt
FileDelete, %A_ProgramFiles%\tracks\racesort.txt
filedelete, %A_ProgramFiles%\tracks\race%race#%.txt
FileDelete, %A_ProgramFiles%\tracks\%trk%.txt

Gui,submit, NoHide
FormatTime,date,%MyCalendar%,MMdd

IfNotExist, %A_ProgramFiles%\tracks\%trk%%date%.pcs
{
MsgBox, 16, File Not Found, The selected PCS file does not exist
Return
}


FileRead, contents, C:\Program files\tracks\%trk%%date%.pcs
StringSplit,TestVar,contents,`,

loop 150
{

        if (A_Index=1)
                {
                Track=1
                DateN = 2
                Race = 3
                Name = 45
                Speed=846
                Lengths=746
                position=616
                }
                else
                {
                 
                Track+=1434
                DateN+= 1434
                Race+= 1434
                Name+= 1434
                Speed+=1425
                Lengths+=1425
                position+=1425
                }

data:=TestVar%Track%
data2:=TestVar%DateN%
data3:=TestVar%Race%
data4:=TestVar%Name%
data5:=TestVar%Speed%
data6:=TestVar%Lengths%
data7:=TestVar%position%


       
                Fileappend, %data%|, %A_ProgramFiles%\tracks\Test.txt
                Fileappend, R%data3%|, %A_ProgramFiles%\tracks\Test.txt
                Fileappend, %data4%|`n, %A_ProgramFiles%\tracks\Test.txt


        Loop 10         ;must be set at 10
       {
                if (A_Index=1)
                        {
                        Speed= %speed%
                        Lengths= %lengths%
                        position=%position%
                        }
                else
                        {
                        Speed+=1
                        Lengths+=1
                        position+=1
                        }

data5:=TestVar%Speed%
data6:=TestVar%Lengths%
data7:=TestVar%position%

    if data6 < 3.1
        Fileappend, %data5%|, %A_ProgramFiles%\tracks\Test.txt
    Else if data7 = 1
        Fileappend, %data5%|, %A_ProgramFiles%\tracks\Test.txt
   
        }
Back to top
View user's profile Send private message
Uminnsky



Joined: 12 Aug 2009
Posts: 26

PostPosted: Sun Aug 23, 2009 12:48 am    Post subject: Reply with quote

I should add I tried putting

MyVar:=
MyVar2:=
...
at the end of the script, to know avail...
Back to top
View user's profile Send private message
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Sun Aug 23, 2009 8:26 am    Post subject: Reply with quote

At the end you mean outside the loop, try moving it to before the last } so the vars are cleared in each iteration of the loop
_________________
AHK Wiki FAQ
TF : Text files & strings lib, TF Forum
Back to top
View user's profile Send private message
Uminnsky



Joined: 12 Aug 2009
Posts: 26

PostPosted: Sun Aug 23, 2009 7:09 pm    Post subject: Reply with quote

Thanks, I need the variables to hold there data until the loop is finished so I can't. It works fine during the initial execution, its just when I run it a second time on a new file that it carries over data from the previous execution.
Back to top
View user's profile Send private message
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Sun Aug 23, 2009 7:39 pm    Post subject: Reply with quote

Then at the TOP of the script of just before/after the fileread?

Edit: or you can Reload the script (see doc page) it will clear all vars
_________________
AHK Wiki FAQ
TF : Text files & strings lib, TF Forum
Back to top
View user's profile Send private message
hd0202



Joined: 13 Aug 2006
Posts: 265
Location: Germany

PostPosted: Sun Aug 23, 2009 8:05 pm    Post subject: Reply with quote

Do you want to tell us, that every "%trk%%date%.pcs" file has 215,100 or more fields, separated by comma? So many must be there as you loop constantly with 150 and use everey 1434th field. If there are less , the residual fields contain the contents of the previous record(s). To avoid this problem use
Code:
loop % TestVar0

Hubert
Back to top
View user's profile Send private message
Uminnsky



Joined: 12 Aug 2009
Posts: 26

PostPosted: Sun Aug 23, 2009 8:59 pm    Post subject: Reply with quote

Thanks Hubert,

The file will usually only have about 157k comma delimated fields, I put 150 just to make sure it doesn't stop short of looping through everything...I understand your point...

However, I am not sure if I am utilzing what you suggested right.

I replaced

Code:
Loop 150


with

Code:
Loop % TestVar0


And it just infinitely loops. What am I missing? Thanks for the help.

Just to clarify what your saying...if I looped a file that had 180k fields, it will store data in TestVar180,000 (or whatever). Now, when I loop through a new file that has 150k fields it keeps looping beyond the end of the file, and since there is nothing past 150k, it just spits out the data from from TestVar180k that was never cleared or rewritten. If I understand you right, you just caused a Idea.

...HugoV, yea, I tried clearing them at the beginning as well. I am using the reload now as a temporary work around, but it is a pain because it resets my calendar/user input as well. Regardless, it is pissing me off because I want to understand why its doing what it's doint Smile
Back to top
View user's profile Send private message
sinkfaze



Joined: 18 Mar 2008
Posts: 5044
Location: the tunnel(?=light)

PostPosted: Sun Aug 23, 2009 9:11 pm    Post subject: Reply with quote

I'm assuming it's the "data" variables that you've set into a pseudo array that are causing the problem? If it is you could just clear the variables with a loop once the label is called:

Code:
ButtonViewPicks:
Loop, 7 {
  if A_Index = 1
    VarSetCapacity(data,0)
  else
    VarSetCapacity(data%A_Index%,0)
}
; MsgBox as error check to verify the variables are now cleared of data
MsgBox % "data = " data "`n" "data2 = " data2 "`n" "data3 = " data3 "`n" "data4 = " data4 "`n" "data5 = " data5 "`n" "data6 = " data6 "`n" "data7 = " data7 "`n"
<< ...script continues... >>

_________________
Try Quick Search for Autohotkey or see the tutorial for newbies.
Back to top
View user's profile Send private message Send e-mail
Uminnsky



Joined: 12 Aug 2009
Posts: 26

PostPosted: Sun Aug 23, 2009 9:34 pm    Post subject: Reply with quote

Ok, I got it...

I added this to the beginning of the script to clear the variables. Thanks for all the help...

Code:
Loop 150 ; Clear all TestVar variables
{
if (A_Index=1)
                {
                Track=1
                DateN = 2
                Race = 3
                Name = 45
                Speed=846
                Lengths=746
                position=616
                }
                else
                {
                 
                Track+=1434
                DateN+= 1434
                Race+= 1434
                Name+= 1434
                Speed+=1425
                Lengths+=1425
                position+=1425
                }

TestVar%Track%:=
TestVar%DateN%:=
TestVar%Race%:=
TestVar%Name%:=
TestVar%Speed%:=
TestVar%Lengths%:=
TestVar%position%:=
}


FileRead, contents, C:\Program files\tracks\%trk%%date%.pcs
StringSplit,TestVar,contents,`,

Loop 150
{

        if (A_Index=1)
                {
                Track=1
                DateN = 2
                Race = 3
                Name = 45
                Speed=846
                Lengths=746
                position=616
                }
                else
                {
                 
                Track+=1434
                DateN+= 1434
                Race+= 1434
                Name+= 1434
                Speed+=1425
                Lengths+=1425
                position+=1425
                }

data:=TestVar%Track%
data2:=TestVar%DateN%
data3:=TestVar%Race%
data4:=TestVar%Name%
data5:=TestVar%Speed%
data6:=TestVar%Lengths%
data7:=TestVar%position%
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group