Can someone explain the example on the 'map and lookup' tips under the 'String manipulation'? http://www.dostips.com/DtTipsStringMani ... _MapLookup
REM ---- Example 2: Translate abbreviation into full string ----
SET v=sun
set map=mon-Monday;tue-Tuesday;wed-Wednesday;thu-Thursday;fri-Friday;sat-Saturday;sun-Sunday
CALL SET v=%%map:*%v%-=%%
SET v=%v:;=&rem.%
ECHO.%v%
I know it works in a script but I failed to make it work line by line on the command prompt(after account for the difference of %% in script vs command line). I know the 'CALL' line is to replace string up to the key value by null and the following line is to replace everything starting from ; to the end by null.
Bu what is the reason for 'CALL'? What will be different if withou using CALL? Why '&rem.'? Primarily, I would like to know how I can use those constructs in other scenario.
Maybe someone has already know about this, for the purpose those 2 examples want to achieve, I have the following code snipet that can do the same(but it can not exactly achieve the goal of 'map and lookup' since the key need to be numeric and in sequence). But I think this is simpler:
set v=05
set Months=Jan;Feb;Mar;Apr;May;Jun;Jul;Aug;Sep;Oct;Nov;Dec
for /f "delims=; tokens=%v%" %%i in ("%Months%") do @echo %%i
-Frank
map and lookup - please explain
Moderator: DosItHelp
Hi Frank,
1. Difference between "Call Set" and "Set"
the call results in a double expansion of the line.
The first expansion results in (with v=sat)
the second expansion will do the rest
%var:xx=yy% replaces all xx with yy in var
%var:*xx=yy% only replaces the first occurence of xx with yy in var, and the results begins with the first occurence (removing of the front)
2.
is a trick to truncate the rest, start of truncation is the ";"
this would only replace the ; with nothing but the rest of the string is not truncated
jeb
Bu what is the reason for 'CALL'? What will be different if withou using CALL? Why '&rem.'?
1. Difference between "Call Set" and "Set"
CALL SET v=%%map:*%v%-=%%
the call results in a double expansion of the line.
The first expansion results in (with v=sat)
Code: Select all
v=%map:*sat-=%
the second expansion will do the rest
Code: Select all
v=Saturday;sun-Sunday
%var:xx=yy% replaces all xx with yy in var
%var:*xx=yy% only replaces the first occurence of xx with yy in var, and the results begins with the first occurence (removing of the front)
2.
Code: Select all
SET v=%v:;=&rem.%
is a trick to truncate the rest, start of truncation is the ";"
Code: Select all
SET v=%v:;=%
this would only replace the ; with nothing but the rest of the string is not truncated
jeb
Jeb,
Thanks for the detailed explaination. Should I interpret *xx as 'a regular expression to match anything up to xx' rather than 'first occurence of xx'?
-Frank
Thanks for the detailed explaination. Should I interpret *xx as 'a regular expression to match anything up to xx' rather than 'first occurence of xx'?
-Frank
jeb wrote:
%var:*xx=yy% only replaces the first occurence of xx with yy in var, and the results begins with the first occurence (removing of the front)
jeb