如何返回批量数组的元素? [英] How to return an element of an array in Batch?

查看:131
本文介绍了如何返回批量数组的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数组列表中的两个元素在我的计划。我该如何分配变量等于要素之一?

I have two elements in my array list in my program. How can I assign a variable to equal one of the elements?

这里的code:

@echo off
setlocal enabledelayedexpansion
set /p string=
for /l %%a in (0,1,1000) do if not "!String:~%%a,1!"=="" set /a length=%%a+1
set i=0
:input
set str=%string:~0,1%
if "%str%"=="M" set array[i]=1000
if "%str%"=="D" set array[i]=500
if "%str%"=="C" set array[i]=100
if "%str%"=="L" set array[i]=50
if "%str%"=="X" set array[i]=10
if "%str%"=="I" set array[i]=1
set string=%string:~1%
set /a i=i+1
if %i%==%length% goto logic
goto input
:logic

我真的,虽然有这样的标准方法。

I really though there was a standard way of doing this.

推荐答案

主要的问题是,你的code不会产生任何的批量阵列。结果
您code只能创建一个名为数组[我] 一个变量,但我想你想创建一个数组:

The main problem is that your code doesn't create any batch-array.
Your code create only one variable named array[i], but I suppose you want to create an array with:

array[0]=1000
array[1]=500

然后,你需要像

setlocal EnableDelayedExpansion
set i=0
:inputLoop
set "str=%string:~0,1%"
if "%str%"=="M" set array[%i%]=1000
if "%str%"=="D" set array[%i%]=500
if "%str%"=="C" set array[%i%]=100
if "%str%"=="L" set array[%i%]=50
if "%str%"=="X" set array[%i%]=10
if "%str%"=="I" set array[%i%]=1
set "string=%string:~1%"
set /a i+=1
if NOT %i%==%length% goto :inputLoop

:logic
rem ** logic begins
for /L %%n in (1 1 %i%) do (
   echo !array[%%n]!
   set /a value=array[%%n]
)

和在逻辑的你可以看到如何访问数组元素的一部分。

And in the logic part you can see how to access an array element.

顺便说一句。你strlen函数有点慢,它可以用一个二进制搜索会更快。结果
如何在批量字符串算上字符?

Btw. Your strlen function is a little bit slow, it could be faster with a binary search.
How to count the characters in a string with Batch?

这篇关于如何返回批量数组的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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