How to clear variables from separate
Moderators: JRL, Dorian (MJT support)
-
- Pro Scripter
- Posts: 149
- Joined: Tue Mar 23, 2004 9:11 pm
How to clear variables from separate
Hi!
How do I clear a variable?
If i do a "Separate" I get variable_1 variable_2 etc.
How do I clear all these. seting Let>variable= does not clear variable_1
Thanks!
How do I clear a variable?
If i do a "Separate" I get variable_1 variable_2 etc.
How do I clear all these. seting Let>variable= does not clear variable_1
Thanks!
- Phil Pendlebury
- Automation Wizard
- Posts: 543
- Joined: Tue Jan 16, 2007 9:00 am
- Contact:
It would help if you added a bit more explanation i.e. when you say "clear" so you mean make it not exist any more or set to zero or what?
Anyway it may help you to know that you can refer to the variables like this example:
Do not use the exact code (obviously) it is an endless loop but just to show how to reference the multiple arr vars.
Hope this helps.

Anyway it may help you to know that you can refer to the variables like this example:
Code: Select all
Label>START
Let>count=1
Let>variable_%count%=WHATEVER YOU WANT
Let>count=count+1
IF>count=GREATER THAN NUMBER OF ARR VARS
GOTO>START
Hope this helps.

Phil Pendlebury - Linktree
Clear variable value vs clear from existence
Use Phil's method to set each of the array members to some value you take to mean "cleared". It is not possible to remove them from existence once created within a macro. Even setting an array member to an empty screen does not really clear it, it means that member's value is the empty string.
In the case of the separate command, you don't really need to clear them.
Let's say you want the array members to be blasted out of existence. The way to do that is to put your separate command into a called macro. The called macro will create the array, do some work, and if you wish pass a result in MACRO_RESULT. When you return to the main macro, the array no longer exists.
Let's say you want to do this with compiled macro's on a machine without Macro Scheduler installed. Do it the same way except use RunProgram to launch the called macro. You will need to find a way other than MACRO_RESULT to return a value, probably STDOUT.
Gale
In the case of the separate command, you don't really need to clear them.
Code: Select all
GetFileList>c:\temp1\*.*,files
Separate>files,;,file_names
MessageModal>Num Files: %file_names_count%
If>file_names_count=0,end1
Let>k=0
Repeat>k
Let>k=k+1
Message>file_names_%k%
Until>k,file_names_count
Label>end1
//Do whatever processing you want with the array
//Do it again for another folder
GetFileList>c:\temp2\*.*,files
Separate>files,;,file_names
MessageModal>Num Files: %file_names_count%
If>file_names_count=0,end2
Let>k=0
Repeat>k
Let>k=k+1
Message>file_names_%k%
Until>k,file_names_count
Label>end2
//the new value of file_names_count will prevent the script from accessing array members captured for the temp1 folder. In a real script you would probalby put this in a gosub and just keep using the array members you need repeatedly.
Code: Select all
//calledmacro.scp
//All variables are new each time this macro is called.
//All variables are local, not visible in mainmacro.
GetFileList>%parmFileList%,files
Separate>files,;,file_names
MessageModal>Num Files: %file_names_count%
If>file_names_count=0,end
Let>k=0
Repeat>k
Let>k=k+1
Message>file_names_%k%
Until>k,file_names_count
Label>end
//Do whatever work you want with the array.
//Write filelist to a file or whatever.
//Send any information you want back to mainmacro
Let>MACRO_RESULT=%file_names_count%
Code: Select all
//mainmacro.scp
Macro>calledmacro.scp /parmFileList=c:\temp1\*.*
MDL>%MACRO_RESULT%
//None of the variables in calledmacro.scp exist.
Macro>calledmacro.scp /parmFileList=c:\temp2\*.*
MDL>%MACRO_RESULT%
//None of the variables in calledmacro.scp exist.
Gale
- Bob Hansen
- Automation Wizard
- Posts: 2475
- Joined: Tue Sep 24, 2002 3:47 am
- Location: Salem, New Hampshire, US
- Contact:
Definition of "clear" not clear
Hi Bob,
Your solution is probably OK for most situations, those where we expect every valid array element to have a nonempty value. As Phil said, rullbandspelare has not yet defined "clear".
Let's say at some time in the past, this command was issued:
Separate>;;1;;2;;3,;,variable
In this case, the fact that variable_1 is empty does not mean it is unassigned and know nothing about it.
The Separate command also generates variable_count and sets it to the number of array members.
Rullbandspelare was attempting to get rid of all the array elements. One way to indicate that previously set array members are to be considered unassigned is to:
Let>variable_count=0.
True, variable_1 will still exist - but now we know to ignore it.
And yes, for most situations this is more complicated and nitty gritty than it needs to be.
Gale
Your solution is probably OK for most situations, those where we expect every valid array element to have a nonempty value. As Phil said, rullbandspelare has not yet defined "clear".
Let's say at some time in the past, this command was issued:
Separate>;;1;;2;;3,;,variable
In this case, the fact that variable_1 is empty does not mean it is unassigned and know nothing about it.
The Separate command also generates variable_count and sets it to the number of array members.
Rullbandspelare was attempting to get rid of all the array elements. One way to indicate that previously set array members are to be considered unassigned is to:
Let>variable_count=0.
True, variable_1 will still exist - but now we know to ignore it.
And yes, for most situations this is more complicated and nitty gritty than it needs to be.
Gale
The members still exist in watchlist
I have not tried using Separate to create a zero length array.
I have tried this:
Use separate to create a 10 member array;
variable_count=10.
Then use separate to create a 5 member array of the same name.
Now variable_count=5.
The members variable_6 thru variable_10 still exist with their original values in watchlist, but we do not look for them because variable_count=5.
We don't really have a variable whose type is "array", rather we have an array of variables whose names are related.
Gale
I have tried this:
Use separate to create a 10 member array;
variable_count=10.
Then use separate to create a 5 member array of the same name.
Now variable_count=5.
The members variable_6 thru variable_10 still exist with their original values in watchlist, but we do not look for them because variable_count=5.
We don't really have a variable whose type is "array", rather we have an array of variables whose names are related.
Gale
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
No need to clear. Any command that returns an array also returns the length of the array. So why would you ever be reading/writing past the end anyway?
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
GoSub does not return a parmeter count.
Separate> and FindImagePos return array member counts.
GoSub> does not return an array member count.
Not sure about any other commands generating arrays.
Gale
GoSub> does not return an array member count.
Not sure about any other commands generating arrays.
Gale
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Re: GoSub does not return a parmeter count.
But by design you would know how many vars your subroutine expects.gdyvig wrote:GoSub> does not return an array member count.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
GoSub scenarioes with varying numbers of params.
Normally you would know the number of parameters for a GoSub.
My co-worker attempted to write a SRT that would accept a variable number of parameters. I think it was a filelist. Because the GoSub did not automatically provide a parameter count we decided it was better to pass one parameter - a semicolon delimited list of filenames.
Another time we had a SRT with 2 params. Later we decided to add an optional 3rd parameter. We wanted the SRT to be backward compatible with existing scripts passing in the original 2 params. Since we were lazy, we did not want to update all the existing scripts to pass an empty 3rd parameter. The solution was to create 2 versions of the SRT. The 2-param version became a wrapper that passed a dummy param to the new version. A param count variable would have made it a little easier.
Gale
My co-worker attempted to write a SRT that would accept a variable number of parameters. I think it was a filelist. Because the GoSub did not automatically provide a parameter count we decided it was better to pass one parameter - a semicolon delimited list of filenames.
Another time we had a SRT with 2 params. Later we decided to add an optional 3rd parameter. We wanted the SRT to be backward compatible with existing scripts passing in the original 2 params. Since we were lazy, we did not want to update all the existing scripts to pass an empty 3rd parameter. The solution was to create 2 versions of the SRT. The 2-param version became a wrapper that passed a dummy param to the new version. A param count variable would have made it a little easier.
Gale