map and lookup - please explain

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
chcfrank
Posts: 10
Joined: 22 May 2008 20:24

map and lookup - please explain

#1 Post by chcfrank » 25 May 2008 08:12

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

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#2 Post by jeb » 25 May 2008 15:36

Hi Frank,

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

chcfrank
Posts: 10
Joined: 22 May 2008 20:24

#3 Post by chcfrank » 26 May 2008 10:09

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

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

Post Reply