cmd.exe(批处理)脚本中的数组、链表等数据结构 [英] Arrays, linked lists and other data structures in cmd.exe (batch) script

查看:61
本文介绍了cmd.exe(批处理)脚本中的数组、链表等数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩 cmd.exe,但在它的帮助中我没有找到任何信息,如何定义数组.

I was playing with cmd.exe, but in its help I didn't find any info, how to define arrays.

我发现,如何定义简单的变量:

I have found, how to define simple variables:

set a=10
echo %a%

但是,我想创建数组、链表等...

But, I want to create arrays, linked list etc...

那么,它是否可以在 cmd.exe 中使用(我的意思是:在 cmd.exe 中是否存在任何数组关键字?)

So, does it able in cmd.exe ( I mean: does in cmd.exe exist any array keywords? )

我想实现一些算法:

  • 冒泡排序
  • 快速排序
  • 侏儒排序

等等...

所以,我也想知道,Cmd.exe 是否有引用或实例、结构等?

So, I also want to know, does Cmd.exe have references or instances, structs etc?

因为它的帮助不是完整的:/?

Cause its help not full in: /?

Cmd.exe 能否被图灵机定义为完整的?(图灵完备)

Could Cmd.exe be defined as full by Turing-Machine definition? ( Turing-Complete )

推荐答案

好的.我会尽量说清楚,以免被误解...

Ok. I'll try to be as clear as possible to not be misunderstood...

在 Windows 批处理文件中,变量名应该以字母开头,并且可以包含任何有效字符,其中有效字符是:#$'()*+,-.?@[]_`{}~ 除了字母和数字.

In Windows Batch files a variable name should begin with a letter and may include any valid character, where valid characters are: #$'()*+,-.?@[]_`{}~ besides letters and digits.

这意味着从cmd.exe来看,SET NORMAL_NAME=123SET A#$'()*+,-.?@[\]_{}~=123SET VECTOR[1]=123 相同;这三个都是正常变量.这样,以数组元素的形式编写变量名称由您决定:

This means that from the cmd.exe point of view, SET NORMAL_NAME=123 is exactly the same as SET A#$'()*+,-.?@[\]_{}~=123 and also the same as SET VECTOR[1]=123; all three are normal variables. This way, it is up to you to write variable names in the form of array elements:

set elem[1]=First element
set elem[2]=Second one
set elem[3]=The third one

这样,echo %elem[2]% 就会显示第二个.

如果你想使用另一个变量作为索引,你必须知道用它们的值替换百分号包围的变量是从左到右解析;这意味着:

If you want to use another variable as index, you must know that the replacement of variables enclosed in percent symbols by their values is parsed from left to right; this means that:

set i=2
echo %elem[%i%]%

没有给出想要的结果,因为这意味着:显示 elem[ 变量的值,然后是 i,然后是 的值] 变量.

doesn't give the desired result because it means: show the value of the elem[ variable, followed by i, followed by the value of the ] variable.

要解决这个问题必须使用Delayed Expansion,即在开头插入setlocal EnableDelayedExpansion命令,将索引变量用百分号括起来,将数组元素括起来在感叹号中:

To solve this problem you must use Delayed Expansion, that is, insert setlocal EnableDelayedExpansion command at the beginning, enclose index variables in percent symbols, and enclose the array elements in exclamation marks:

setlocal EnableDelayedExpansion
set elem[1]=First element
set elem[2]=Second one
set elem[3]=The third one
set i=2
echo !elem[%i%]!

你也可以使用 FOR 命令的参数作为索引:for/L %%i in (1,1,3) do echo !elem[%%i]!.您必须使用 !index!在 FOR 或 IF 中更改索引时将值存储在数组元素中:set elem[!index!]=New value.要在 FOR/IF 内的索引更改时获取元素的值,请将元素括在双百分号中,并在命令之前使用 call.例如,将一系列数组元素向左移动四位:

You may also use parameters of FOR commands as indexes: for /L %%i in (1,1,3) do echo !elem[%%i]!. You must use !index! to store values in array elements when the index is changed inside a FOR or IF: set elem[!index!]=New value. To get the value of an element when the index changes inside FOR/IF, enclose the element in double percent symbols and precede the command with call. For example, to move a range of array elements four places to the left:

for /L %%i in (%start%,1,%end%) do (
   set /A j=%%i + 4
   call set elem[%%i]=%%elem[!j!]%%
)

实现前面过程的另一种方法是使用额外的FOR命令通过等效的可替换参数改变索引的延迟扩展,然后对数组元素使用延迟扩展.这个方法比之前的 CALL 运行得更快:

Another way to achieve the previous process is to use an additional FOR command to change the delayed expansion of the index by an equivalent replaceable parameter, and then use the delayed expansion for the array element. This method runs faster than previous CALL:

for /L %%i in (%start%,1,%end%) do (
   set /A j=%%i + 4
   for %%j in (!j!) do set elem[%%i]=!elem[%%j]!
)

这样,批处理文件行为就像它管理数组一样.我认为这里的重点不是讨论 Batch 是否管理数组,而是您可以以与其他编程语言等效的方式管理 Batch 文件中的数组.

This way, the Batch file behaves like it manages arrays. I think the important point here is not to discuss if Batch manages arrays or not, but the fact that you may manage arrays in Batch files in an equivalent way of other programming languages.

@echo off
setlocal EnableDelayedExpansion

rem Create vector with names of days
set i=0
for %%d in (Sunday Monday Tuesday Wednesday Thrusday Friday Saturday) do (
   set /A i=i+1
   set day[!i!]=%%d
)

rem Get current date and calculate DayOfWeek
for /F "tokens=1-3 delims=/" %%a in ("%date%") do (
   set /A mm=10%%a %% 100, dd=10%%b %% 100, yy=%%c
)
if %mm% lss 3 set /A mm=mm+12, yy=yy-1
set /A a=yy/100, b=a/4, c=2-a+b, e=36525*(yy+4716)/100, f=306*(mm+1)/10, jdn=c+dd+e+f-1523, dow=jdn %% 7 + 1
echo Today is !day[%dow%]!, %date%

注意索引值不限于数字,可以是任何包含有效字符的字符串;这一点允许定义在其他编程语言中被称为关联数组的内容.在这个答案中有一个使用关联数组解决问题的方法的详细说明.还要注意空格是变量名中的有效字符,所以一定要注意不要在变量名中插入空格,否则可能会被忽视.

Note that index values are not limited to numbers, but they may be any string that contain valid characters; this point allows to define what in other programming languages are called associative arrays. At this answer there is a detailed explanation of the method used to solve a problem using an associative array. Note also that the space is a valid character in variable names, so you must pay attention to not insert spaces in variable names that may go unnoticed.

我在详细说明了必须在批处理文件中使用数组表示法的原因这个帖子.

这篇文章中有一个批处理文件读取文本文件并将行的索引存储在向量中,然后根据行内容对向量元素进行冒泡排序;等效的结果是对文件内容进行排序.

In this post there is a Batch file that reads a text file and stores the indexes of the lines in a vector, then does a Buble Sort of vector elements based on line contents; the equivalent result is a sort over file contents.

这篇文章中有一个基本的关系基于存储在文件中的索引的批处理数据库应用程序.

In this post there is a basic Relational Data Base application in Batch based on indexes stored in files.

这篇文章中有一个完整的多个Batch 中的链表应用程序,用于组装从子目录中获取的大型数据结构,并以 TREE 命令的形式显示.

In this post there is a complete multiple linked-list application in Batch that assembles a large data structure taken from a subdirectory and displays it in the form of TREE command.

这篇关于cmd.exe(批处理)脚本中的数组、链表等数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆