Thinking of a "supernatural" batch and adding a note _dos/bat

Source: Internet
Author: User
Tags extend goto
The requirement for batch processing is that the randomly displayed number (6,7,8,9,10,11,12,14,15,16,17) is one of the

Note: There is no 13 in the

The following two code, the first error, the second is successful, but their difference is only the first (%random%) percent (%n%) +1 after the value given to%tn%, and the second will continue to give the value of the Operation%n% ...
Copy Code code as follows:

@echo off
Set "String=6 7 8 9 10 11 12 14 15 16 17"
For%%i in (%string%) does call set/a "N=%%n%%+1"
set/a "tn= (%random%)% (%n%) +1"
for/f "Usebackq tokens=%tn% delims="%%i in ('%string% ') do echo%%i
Pause
Goto:eof

Copy Code code as follows:

@echo off
Set "String=6 7 8 9 10 11 12 14 15 16 17"
For%%i in (%string%) does call set/a "N=%%n%%+1"
set/a "n= (%random%)% (%n%) +1"
for/f "Usebackq tokens=%n% delims="%%i in ('%string% ') do echo%%i
Pause
Goto:eof

found that set/a "tn= (%random%)% (%n%) +1" In this statement the assigned variable name can only be one character, more than one character can be faulted (tested, and for-independent), but this error will not occur if the quotation marks on either side of the assignment expression are also removed!

be mad to pour ...
For%%i in (%string%) does call set/a "N=%%n%%+1"

This statement allows me to understand for a very half-day:
We know that batch processing in the process of reading each statement/execute each command will expand the statement/command variables.
Let's take a look at what's going on with this example:
When reading a for statement, the%%n%% is extended to%n%, which is the call set/a "N=%n%+1".
At this point we may think that direct set/a "n=%n%+1" not on the line, call is superfluous.
However, in this case, in fact, if the Call,set command is omitted, the prompt "cannot find the operand." ”
Why, then? Because the read for is expanded once, the for statement deprives the right of the first command to extend the variable.
So, after omitting call, set no longer expands%n%, thinking that%n% is not a number, so it goes wrong.
After call, the SET command expands the%n% again, and the set operation can be performed even if the%n% is extended to null without assignment.
That is, call does not have an extension variable, it only plays the role of separating the set command from the For statement, so that the set is not being stripped of the power to extend the variable.
Of course, you think call expansion can also be, the effect is the same, although the actual process is different.
When%%i=6, the%n% is expanded to empty and the set assigns N to 1
When%%i=7,%n% is extended to 1,set to assign N to 2
When%%i=8,%n% is extended to 2,set to assign N to 3
Analogy

For example, understand:
Set n=123
For%%i in (1) do echo%%n%%
Pause

When we run this batch, we see the For%i in (1) do echo%n%
That is to say, when reading the for statement,%%n%% has been extended to%n%
If Echo also has the power to extend the variable, echo%n% should display the true value of%n% 123
In fact, Echo shows "%n%" exactly.
Description, for the power of the echo extension variable is stripped.

Of course, you must be directly for%%i in (1) do echo%n%
However, this "supernatural" batch is meant to refer to a variable that is not being expanded, and to be augmented in do, that is, to achieve the effect of delaying the environment variable. The goal is to use an incrementing variable to count how many characters the%string% contains.

I to the six-winged hedgehog's infinite reverence, like the surging Yangtze River, continuous, such as the Yellow River flooding, a turn and out of hand!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.