How to clear an array?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

How to clear an array?

#1 Post by SIMMS7400 » 28 May 2023 08:29

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

einstein1969
Expert
Posts: 960
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: How to clear an array?

#2 Post by einstein1969 » 28 May 2023 09:28

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.

Eureka!
Posts: 137
Joined: 25 Jul 2019 18:25

Re: How to clear an array?

#3 Post by Eureka! » 28 May 2023 14:54

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="

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to clear an array?

#4 Post by Aacini » 29 May 2023 00:14

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

Eureka!
Posts: 137
Joined: 25 Jul 2019 18:25

Re: How to clear an array?

#5 Post by Eureka! » 29 May 2023 07:26

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.

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to clear an array?

#6 Post by Aacini » 29 May 2023 12:02

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

Eureka!
Posts: 137
Joined: 25 Jul 2019 18:25

Re: How to clear an array?

#7 Post by Eureka! » 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

IcarusLives
Posts: 175
Joined: 17 Jan 2016 23:55

Re: How to clear an array?

#8 Post by IcarusLives » 29 May 2023 16:43

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="

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to clear an array?

#9 Post by Aacini » 31 May 2023 23:07

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

shodan
Posts: 89
Joined: 01 May 2023 01:49

Re: How to clear an array?

#10 Post by shodan » 20 Aug 2023 13:21

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

Post Reply