Thoughts raised by a batch processing of "flexibility"

Source: Internet
Author: User

The batch processing requirement is that the numbers displayed randomly are (6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17) one of them.

Note: There are no 13

The following twoCode, The first error, but the second one succeeded, but their difference is only the first (% random %) % (% N %) + The value after 1 operation is assigned to % TN %, and the second value is assigned to % N % ......Copy codeThe Code is as follows: @ echo off
Set "string = 6 7 8 9 10 11 12 14 15 16 17"
For % I in (% string %) Do 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 codeThe Code is as follows: @ echo off
Set "string = 6 7 8 9 10 11 12 14 15 16 17"
For % I in (% string %) Do 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

We found that the variable name assigned to the set/a "Tn = (% random %) % (% N %) + 1" statement can only be one character, an error occurs when there are more than one character (tested and irrelevant to for). However, if you remove the quotation marks on both sides of the value expression, this error will not occur!

Crazy ......
For % I in (% string %) Do call set/a "n = % N % + 1"

This statement makes me understand it for a long time:
We know that the variable in the statement/command is expanded once every statement/command is read/executed during batch processing.
Let's take a look at what happened in this example:
When the for statement is read, % N % is expanded to % N %, that is, the do is call set/a "n = % N % + 1 ".
In this case, we may think that it is okay to directly set/a "n = % N % + 1", and call is an extra action.
However, in this example, if call is omitted, the set command will encounter an error and prompt "the operand cannot be found ."
Why? Because the for statement has been extended once when reading the for statement, the for statement will deprive do of the power of the First Command to expand the variable.
Therefore, after the call is omitted, the set does not expand % N %, and % N % is considered not a number, so an error occurs.
After the call, the set command expands % N % again. Even if % N % is not assigned a value, it can be expanded to null. In this case, you can perform the set operation.
That is, call does not expand the variable, but serves to separate the SET command from the for statement, so that the set is not deprived of the power of the extended variable by.
Of course, you can extend the call. The effect is the same, although the actual process is different.
When % I = 6, % N % is expanded to null. Set assigns n to 1.
When % I = 7, % N % is expanded to 1. Set assigns n to 2.
When % I = 8, % N % is expanded to 2, and set assigns n to 3
And so on.

For example:
Set n = 123
For % I in (1) Do echo % N %
Pause

When running this batch, we see for % I in (1) Do echo % N %
That is, when the for statement is read, % N % has been extended to % N %
If Echo also has the power to expand the variable, Echo % N % should display the true value of % N % 123
In fact, "% N %" is displayed in the original echo version"
Note: For has deprived the echo extended variable of power.

Of course, you must directly for % I in (1) Do echo % N %
However, this batch processing is intended to reference a variable not extended by for, but also to be extended in do, that is, to achieve the effect of delayed environment variables. The purpose is to use an incremental variable to count the characters in % string %.

The infinite reverence for the six-wing hedgehog, like the Yangtze River, is endless, and like the flood of the Yellow River!

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.