Some Explaination on these

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Flora
Posts: 5
Joined: 28 Jun 2013 09:18

Some Explaination on these

#1 Post by Flora » 07 Jul 2013 23:37

My very first thread on Dostips :P

Hi,i'm looking for some explaination on few of the code blocks here

Source : http://www.zhihua-lai.com/acm/get-strin ... ows-batch/

Code: Select all

@echo off
:: strlen.bat
:: http://acm.zhihua-lai.com
 
if [%1] EQU [] goto end
 
:loop
    if [%1] EQU [] goto end
    set _len=0
    set _str=%1
    set _subs=%_str%
 
    :getlen     
        if not defined _subs goto result
        :: remove first letter until empty
        set _subs=%_subs:~1%
        set /a _len+=1
        goto getlen
 
    :result
        echo strlen("%1")=%_len%       
        shift
        goto loop
:end


How can the loop manage to exit properly?
from what i can understand, there is no change on
the var _subs (apart from substracting the first character)
Only _len is changing (increment for every loop.) :?

this is from dostips string manipulation section.

Code: Select all

REM ---- Example 1: Translate name of month into two digit number ----
SET v=Mai

SET map=Jan-01;Feb-02;Mar-03;Apr-04;Mai-05;Jun-06;Jul-07;Aug-08;Sep-09;Oct-10;Nov-11;Dec-12
CALL SET v=%%map:*%v%-=%%
SET v=%v:;=&rem.%

ECHO.%v%


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%


in particular

Code: Select all

CALL SET v=%%map:*%v%-=%%
SET v=%v:;=&rem.%


what does this two lines do?
and can we really use CALL SET (some examples if possible)
i thought call is for only calling
labels and external files. :?
Last edited by Flora on 08 Jul 2013 00:22, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Some Explaination on these

#2 Post by foxidrive » 07 Jul 2013 23:58

Flora wrote:How can the loop manage to exit properly?
from what i can understand, there is no change on
the var _subs (apart from substracting the first character)
Only _len is changing (increment for every loop.) :?


put an ECHO %_subs% in the loop and see what is happening to it. Make the string more than 5 characters to see it more clearly.

The routine doesn't handle special characters like & so it pretty limited.

in particular

Code: Select all

CALL SET v=%%map:*%v%-=%%
SET v=%v:;=&rem.%


what does this two lines do?
and can we really use CALL SET (some examples if possible)
i thought call is for only calling
labels and external files. :?


When calling a set command: it executes the line in a child CMD process and the %% become a single % while the %variable% is evaluated, and so it is used to enable variables to be used in these SET commands.

Call was not designed to be used this way but it is a Microsoft Undocumented Feature (MUF) :) and many batch files rely on undocumented features.

The second line replaces all ; with &rem.
Use echo on and run the snippet and it may become more clear.

Flora
Posts: 5
Joined: 28 Jun 2013 09:18

Re: Some Explaination on these

#3 Post by Flora » 08 Jul 2013 00:52

foxidrive wrote:
Flora wrote:How can the loop manage to exit properly?
from what i can understand, there is no change on
the var _subs (apart from substracting the first character)
Only _len is changing (increment for every loop.) :?


put an ECHO %_subs% in the loop and see what is happening to it. Make the string more than 5 characters to see it more clearly.

The routine doesn't handle special characters like & so it pretty limited.


Yeah,i could see the output as expected.But why? how can the variable change?
Let me try to put it this way :

say i have set var_1=amd
set var_1=%var_1:~1% will extract the first character from var_1 and set it to itself.
this will only results in the value of var_1 being set to a.Nothing more, regardless of how many times it is executed in the loop.
Is my logic wrong?

good reply on the call function though,no question there.

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Some Explaination on these

#4 Post by penpen » 08 Jul 2013 01:11

Flora wrote:say i have set var_1=amd
set var_1=%var_1:~1% will extract the first character from var_1 and set it to itself.
(...)
Is my logic wrong?

Yes, your logic is wrong:
The command will extract all characters starting with index one from var_1,
so the first char with index 0 was deleted.


foxidrive wrote:When calling a set command: it executes the line in a child CMD process(...)

This is not correct: Call calls the actual command processor in the actual cmd instance, but escapes its parameter one (additional) time, so the effect is as described.
If it were like you said, it won't work, you can check this manually:

Code: Select all

Z:\>set TEST=THIS CMD INSTANCE

Z:\>cmd
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

Z:\>set TEST=CHILD INSTANCE

Z:\>echo %TEST%
CHILD INSTANCE

Z:\>exit

Z:\>echo %TEST%
THIS CMD INSTANCE

Z:>

penpen

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Some Explaination on these

#5 Post by foxidrive » 08 Jul 2013 01:18

Thanks for the correction penpen.

Flora
Posts: 5
Joined: 28 Jun 2013 09:18

Re: Some Explaination on these

#6 Post by Flora » 08 Jul 2013 01:36

I think this solves all my questions.
Thank you @penpen and @foxydrive for your reply.

Sponge Belly
Posts: 216
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

Re: Some Explaination on these

#7 Post by Sponge Belly » 09 Jul 2013 18:26

Hello Flora! :-)

This line of code is a clever but ugly hack: :twisted:

Code: Select all

SET v=%v:;=&rem.%


It has the effect of discarding everything in the string after the first occurrence of semi-colon (inclusive). It is the opposite of:

Code: Select all

SET v=%v:*;=%


which removes everything before the first occurrence of a semi-colon (semi-colon included).

It’s an ugly hack because it chokes on special characters and won’t work while delayed expansion is enabled. Only use it whenyou know the text it will be working on (as in the months example).

Hope this helps! ;-)

- SB

Post Reply