-
Notifications
You must be signed in to change notification settings - Fork 64
/
array-assoc-2.bat
108 lines (86 loc) · 2.11 KB
/
array-assoc-2.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
@echo off
::
:: The Batchography book by Elias Bachaalany
::
setlocal enabledelayedexpansion
set FRUITS=k@kiwi$a@apple$b@banana$o@orange
call :get-by-key2 FRUITS a R
echo get-by-key2 result=%R%
call :get-by-key FRUITS b R
echo get-by-key result=%R%
call :get-by-key2 FRUITS a R
echo get by key result=%R%
echo Total encoding
echo -------------------
call :total-encode
echo.
echo Explain
echo -------------------
call :explain
echo.
echo Outer encoding
echo -------------------
call :outer-encode
goto :eof
:: ------------------------------
:old-style
set FRUITS[b]=banana
set FRUITS[a]=apple
set FRUITS[o]=orange
set FRUITS[k]=kiwi
goto :eof
:: ------------------------------
:total-encode
for %%a in (%FRUITS:$= %) do (
for /f "usebackq tokens=1,2 delims=@" %%b in ('%%a') do (
echo key=%%b value=%%c
)
)
goto :eof
:: ------------------------------
:outer-encode
for %%a in ("k=kiwi" "a=apple" "b=banana" "o=orange") do (
for /f "usebackq tokens=1,2 delims==" %%b in ('%%~a') do (
echo key=%%b value=%%c
)
)
goto :eof
:explain
echo FRUITS split by space characters: %FRUITS:$= %
for %%a in (%FRUITS:$= %) do (
echo %%a
)
goto :eof
:: ------------------------------
:get-by-key <1=assoc-array-name> <2=key> => <3=result>
setlocal
call set __t=$%%%~1%%$
set __t=!__t:*$%~2@=!
for /f "useback tokens=1* delims=$" %%a in ('%__t%') do (
endlocal
set %~3=%%a
)
:: Method 1
rem set __t=!__t:*$%~2@=!&set __t=!__t:$=!
rem endlocal & set %~3=%__t%
goto :eof
:: ------------------------------
:get-by-key1 <1=assoc-array-name> <2=key> => <3=result>
setlocal
:: Expand the argument value
call set __t=$%%%~1%%$
:: Replace left-hand side k/v pairs
set __t=!__t:*$%~2@=!
:: Trim trailing k/v pairs
set __t=!__t:*$%~2@=!&set __t=!__t:$=!
endlocal & set %~3=%__t%
goto :eof
:: ------------------------------
:get-by-key2 <1=assoc-array-name> <2=key> => <3=result>
setlocal
call set __t=$%%%~1%%$
:: Method 2
set __t=!__t:*$%~2@=!
set __t=!__t:$=^&::!
endlocal & set %~3=%__t%
goto :eof