Documentation SETARRAY & SETLOCAL/ENDLOCAL

May 20, 2008
473
2
It appears SETLOCAL/ENDLOCAL does not remove the SETARRAY created within it.
Code:
C:\>ver

TCC  16.01.45   Windows 7 [Version 6.1.7601]

C:\>echo %@arrayinfo[TestA]
-1

C:\>setlocal

C:\>setarray TestA[5]

C:\>endlocal

C:\>echo %@arrayinfo[TestA]
1

C:\>setarray TestA[5]
TCC: Array variable is already defined "TestA"

Since SET is used to both modify the environment and to modify array elements, I had expected the array to have been treated as part of the environment saved by SETLOCAL, which also includes aliases and functions. My preference is for the ENDLOCAL to remove any array variables created after the SETLOCAL. However, the documentation for the xARRAY and xLOCAL commands does not say one way or the other. What is supposed to happen? Do we need this check before every SETARRAY?
Code:
if %@arrayinfo[ArrayName] NE -1 unsetarray ArrayName
 
May 20, 2008
3,515
4
Elkridge, MD, USA
Sorry, Tim, the 4th paragraph of HELP -> SETLOCAL is
SETLOCAL does not save the command history or array variables.
Ever since version 10... You are right that no similar indication is made in topic SETARRAY, but isn't its lack a hint?
BTW, while ALIAS mentions its relationship to SETLOCAL, there is no mention for FUNCTION.

If you feel strongly about extending the scope of SETLOCAL to SETARRAY, I'd support it in principle, but backward compatibility (for users who expect array definitions to survive ENDLOCAL) would be required. One possible method would be to use SETARRAY /L to define LOCAL arrays, i.e., arrays that must be defined within a SETLOCAL / ENDLOCAL pair. Another quirk is implied ENDLOCAL, i.e., a batch program with explicit SETLOCAL that performs ENDLOCAL on exit from the batch, whether or not present. I myself use implied ENDLOCAL whenever possible (laziness, probably not good programming practice).
 
May 20, 2008
473
2
Thanks, Steve. I don't view not mentioning something as an indication of something else. I like the idea of the SETARRAY /L for local. All of my batch files use the implied ENDLOCAL also. Another variation of SETARRAY I would like is for it to redefine a previously defined array instead of triggering an error.
 
May 20, 2008
3,515
4
Elkridge, MD, USA
I agree that being able to redefine an array without making sure it does not exist COULD be nice - but not always. An option (e.g. /F for forced definition) could take care of that. Are you going to submit them to "feedback"?
 
May 20, 2008
473
2
The /F option should work. I'll let you add it to the /L suggestion. Is there still the other suggestion/vote mechanism? I didn't see it linked from the main site.
 
May 20, 2008
473
2
Now I see it. When I'm typing this text, I literally cannot see it. I had to force myself to check the margins. I normally try to ignore the margins of pages; that's where the ads are. ;)
 
Similar threads
Thread starter Title Forum Replies Date
Dmitry L. Kobyakov Fixed The SETARRAY /R command works incorrectly with cyrillic files Support 2
vefatica SETARRAY - /F not honored with /R Support 2
D Documentation SETARRAY and UNSETARRAY ... Support 1
Joe Caverly SETARRAY Maximums Support 2
x13 SETARRAY /Z bug Support 6
R Is this a bug in SETARRAY? Support 2
J WAD setarray "/f" only works on first one Support 1
J WAD "setarray /f" only overwrites the first array var Support 2
S Fixed SETARRAY /R appears not to close the file Support 4
S SETARRAY - minor issue Support 0
FreezerBurnt "Missing SETLOCAL" when initializing Visual Studio Command Prompt. Support 9
K seems to have no effec tin CMDebug: SETLOCAL EnableDelayedExpansion Support 1
cgunhouse SETLOCAL not working in BDEBUGGER Support 9
Joe Caverly How to? SETLOCAL Level Support 16
Joe Caverly SETLOCAL removes Aliases and Functions Support 3
vefatica SETLOCAL ... ALIAS ... pipes Support 2
rps WAD Setlocal issue Support 4
Charles Dye Read-only environment variables wiped by SETLOCAL / ENDLOCAL Support 5
vefatica Command line SETLOCAL? Support 11
Charles Dye Fixed GPF in SETLOCAL Support 16
M How to? Does the "Do" command effectively do a "SetLocal/EndLocal"??? Support 2
H Help for SETLOCAL in TCMD11 Support 0
Peter Murschall v27.00.16/17: ENDLOCAL with exporting variables wipes out the complete Environment Support 2
Peter Murschall OPTION //directive=value is a ENDLOCAL Support 3
Joe Caverly Update ENDLOCAL to include LIBRARY Support 0
H EndLocal in DO Loop Support 2
Peter Bratton endlocal doesn't work on arrays? Support 1

Similar threads