Page 1 of 1

How to clear an array?

Posted: 28 May 2023 08:29
by SIMMS7400
Hi Guys -

This may be a silly question, but is there a way to clear an array without looping through and explicitly clearing each value?

THis is what I'm doing now:

Code: Select all

    REM Clear Array
		FOR /L %%A IN (1,1,100) DO (
			SET "STR[DC%%~A].DLR="
			SET "STR[DC%%~A].LOC="
			SET "STR[DC%%~A].IMP="
	)
I'm hoping there's a way to just completely clear STR[.

Re: How to clear an array?

Posted: 28 May 2023 09:28
by einstein1969
I don't think there is a method, at least you can do a "setlocal" before defining the variables. With an "endlocal" you clear them up.

Re: How to clear an array?

Posted: 28 May 2023 14:54
by Eureka!
Start your variables with some character-combination that you know does not exist elsewhere yet.
Example with "__" (that's what I use)

Defining:

Code: Select all

set __var1=abc
set __var2=123

Cleaning:

Code: Select all

for /f "usebackq tokens=1 delims==" %%x in (`set __`) do set "%%x="

Re: How to clear an array?

Posted: 29 May 2023 00:14
by Aacini
IMHO it is simpler and clearer to use the standard array notation to define arrays. For example:

Code: Select all

set var[1]=abc
set var[2]=123
In this way you can delete such array in the "standard way":

Code: Select all

for /f "delims==" %%x in ('set var[') do set "%%x="
Otherwise someone may think, especially beginners, that arrays in Batch files must be written with a double-underscore at beginning (or a dollar-sign, or any other special character that have no relation with the standard array notation). Doing that is just confusing (instead of useful).

Antonio

Re: How to clear an array?

Posted: 29 May 2023 07:26
by Eureka!
I think you are unrightfully critical ...

I deliberately did ignore @ SIMMS7400's array variable structure to make it a more general solution.

People who make use of array variables will have zero problems making the translation to adapt it to their situation. No need to explicitly specify that.
Whereas the mentioned syntax can help "beginners" in other situations too.

Re: How to clear an array?

Posted: 29 May 2023 12:02
by Aacini
I don't see any advantage in using your proposed notation to write arrays (and I do see several if the standard form is used).

For example, if you want to use part of a susbcript, I think that

Code: Select all

set "value=!var[%index:~0,4%]!"
... is clearer than

Code: Select all

set "value=!__var%index:~0,4%!"
... because in the first case the subscript is clearly delimited by the square braquets.

If you want to replace part of an array element, I think that

Code: Select all

set "value=!var[%index%]:old=new!"
... is clearer than

Code: Select all

set "value=!__var%index%:old=new!"
... because in the second case there is not any clear delimiter of the parts, besides the characters of the notation themselves.

But perhaps you could point me to some advantage of your "more general" notation...

Antonio

Re: How to clear an array?

Posted: 29 May 2023 12:26
by Eureka!
Aacini wrote:
29 May 2023 12:02
don't see any advantage in using your proposed notation to write arrays
It is *not* an array notation.
Maybe you understand better with

Code: Select all

set __somevar=abc
set __anothervar=123

Re: How to clear an array?

Posted: 29 May 2023 16:43
by IcarusLives
Eureka! wrote:
29 May 2023 12:26
Aacini wrote:
29 May 2023 12:02
don't see any advantage in using your proposed notation to write arrays
It is *not* an array notation.
Maybe you understand better with

Code: Select all

set __somevar=abc
set __anothervar=123
I personally think your way of doing it is very interesting!

I like to do both ways! Depends on what I am doing. Let me explain

Code: Select all

set "this.x="
set "this.y="
set "this.degree="
set "this.color=

and also,

set "var[1]=foo"
set "var[2]=bar"
I think it depends on the data structure that I decide to go with.

If it is an array I will be looping through, I do standard notation.
If it is an array where each variable contributes to a larger thing (like giving a ball a position and color and direction), I do it the other way.

But, I digress, sorry OP.

You can easily do the same in either case..

Code: Select all

for /f "delims==" %%i in ('set this.') do set "%%i="

or

for /f "delims==" %%i in ('set var[') do set "%%i="

Re: How to clear an array?

Posted: 31 May 2023 23:07
by Aacini
IcarusLives wrote:
29 May 2023 16:43

. . . . .

I like to do both ways! Depends on what I am doing. Let me explain

Code: Select all

set "this.x="
set "this.y="
set "this.degree="
set "this.color=

and also,

set "var[1]=foo"
set "var[2]=bar"
I think it depends on the data structure that I decide to go with.

If it is an array I will be looping through, I do standard notation.
If it is an array where each variable contributes to a larger thing (like giving a ball a position and color and direction), I do it the other way.

But, I digress, sorry OP.

You can easily do the same in either case..

Code: Select all

for /f "delims==" %%i in ('set this.') do set "%%i="

or

for /f "delims==" %%i in ('set var[') do set "%%i="

Mmmm... These are not "ways" to do things that "depends on the data structure that you decide to go with"... These are different types of "data aggregates", that is, a data that contain more than a single value. There are several well-specified types of data aggregates that are used to solve specific code-writting problems. For example, an array have this form:

Code: Select all

set "var[1]=foo"
set "var[2]=bar"
A structure have this general notation: structName.memberName, like in your example:

Code: Select all

set "this.x="
set "this.y="
set "this.degree="
set "this.color=
You can also access dynamic structures allocated via an allocation routine that uses a dynamic pointer, like in:

Code: Select all

set "%pointer%->memberName=value" 
And using dynamic pointers you can assemble other data aggregates, like linked lists, etc. All these forms of define and use data aggregates in a Batch file have been described at this thread.

Antonio

Re: How to clear an array?

Posted: 20 Aug 2023 13:21
by shodan
Hello,

For this purpose I have made a function

Code: Select all

:: Usage Call :ClearVariablesByPrefix myPrefix
:ClearVariablesByPrefix
if "[%~1]" NEQ "[]" for /f "tokens=1,2 delims==" %%a in ('set %~1 2^>nul') do set %%a=
if "[%~2]" NEQ "[]" shift & GoTo :ClearVariablesByPrefix
GoTo :EOF
I use it like this

Code: Select all

Call :ClearVariablesByPrefix %_CommandToArray_prefix% _CommandToArray
It is quite fast and safe