Best way to setup a "Dictionary" similar to VBA?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
jfl
Posts: 226
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

Re: Best way to setup a "Dictionary" similar to VBA?

#16 Post by jfl » 22 Feb 2021 07:33

Except that with real files, it's much slower, and very fragile: If other files have one of these extensions, the code will break.

T3RRY
Posts: 243
Joined: 06 May 2020 10:14

Re: Best way to setup a "Dictionary" similar to VBA?

#17 Post by T3RRY » 28 Feb 2021 11:01

Aacini wrote:
21 Feb 2021 23:11
CJM wrote:
21 Feb 2021 19:12
For a quick lookup list, I use the name.ext variable parsers %~n and %~x, adding a . before the lookup value like this example which expands the 3-letter abbreviation for day-of-week to the full word:

Code: Select all

@	FOR %%T in (	%DATE%
)do @	FOR %%D in (
			Sunday.Sun
			Monday.Mon
			Tuesday.Tue
			Wednesday.Wed
			Thursday.Thu
			Friday.Fri
			Saturday.Sat
)do @	IF .%%T==%%~xD ECHO/%%~nD
It has a few obvious character limitations such as:
: \ ? *
but for simple lookups, it's quick and easy.
This method is interesting and may be extended to use real disk files, like this:

Code: Select all

@echo off
setlocal

rem Create the array-like table as empty files on disk
for %%a in ( Sunday.Sun
             Monday.Mon
             Tuesday.Tue
             Wednesday.Wed
             Thursday.Thu
             Friday.Fri
             Saturday.Sat ) do rem/> %%a

:next
echo/
set /P "ext=Enter 3-letters day: "
if errorlevel 1 pause & goto :EOF

rem Do the mapping
for %%a in (*.%ext%) do set "name=%%~Na"

echo Full day: %name%
goto next
Antonio
Interesting Idea. I Doubt the below is any more effecient than just using variables, however it is a somewhat different way of getting values from an array in batch

Code: Select all

@Echo off & CD /D "%~dp0"
 Setlocal EnableDelayedExpansion
 break>"Dictionary.txt"
 Set "Map=Monday.[1] Tuesday.[2] Wednesday.[3] Thursday.[4] Friday.[5] Saturday.[6] Sunday.[7]"
 For %%G in (%Map%)Do >>"Dictionary.txt" Echo(%%G
(Set \n=^^^

%= \n macro newline var. Do Not Modify =%)
%= Define GetIndexMacro; Intiate Getargs    =% Set GetIndex=For %%n in (1 2)Do if %%n==2 (%\n%
%= Iterate over each pending assignment     =%  For %%G in (^^!Args^^!) Do (%\n%
%= Split current returnvar and Index Target =%  For /F "Tokens=1,2 Delims==" %%i in ("%%~G")Do (%\n%
%= Capture Indexed String for assignment    =%    For /F "Delims=" %%V in ('%__APPDIR__%findstr.exe /LIC:".[%%j]" "Dictionary.txt"')Do (%\n%
%= Assign to returnvariable                 =%     set "%%i=%%~nV"%\n%
%= Close Command Loops                      =%  )))%\n%
%= Catch Args for returnvarEQUindex         =% )Else Set Args=

%GetIndex% "Return1=1" "Return2=3"
Set Return

Post Reply