How to clear variables from separate

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
rullbandspelare
Pro Scripter
Posts: 149
Joined: Tue Mar 23, 2004 9:11 pm

How to clear variables from separate

Post by rullbandspelare » Sat May 02, 2009 7:33 pm

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!

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Sat May 02, 2009 7:57 pm

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:

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
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.

:-)
Phil Pendlebury - Linktree

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

Clear variable value vs clear from existence

Post by gdyvig » Sat May 02, 2009 8:27 pm

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.

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.
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.

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.

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

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Sun May 03, 2009 1:23 am

seting Let>variable= does not clear variable_1
Try this: Let>variable_1= to clear variable_1.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

Definition of "clear" not clear

Post by gdyvig » Sun May 03, 2009 2:23 am

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

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Sun May 03, 2009 2:36 am

If you separate nothing into the same array then the old array ceases to exist and the new array has a length of zero. (But I'm not sure why it would ever be necessary to do this.)

Separate>,;,variable

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

The members still exist in watchlist

Post by gdyvig » Sun May 03, 2009 3:49 am

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

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon May 04, 2009 4:24 pm

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?

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

GoSub does not return a parmeter count.

Post by gdyvig » Mon May 04, 2009 5:03 pm

Separate> and FindImagePos return array member counts.

GoSub> does not return an array member count.

Not sure about any other commands generating arrays.

Gale

User avatar
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.

Post by Marcus Tettmar » Mon May 04, 2009 5:49 pm

gdyvig wrote:GoSub> does not return an array member count.
But by design you would know how many vars your subroutine expects.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

GoSub scenarioes with varying numbers of params.

Post by gdyvig » Mon May 04, 2009 6:07 pm

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

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts