Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 数组在Vbscript中循环后丢失值_Arrays_Loops_Vbscript - Fatal编程技术网

Arrays 数组在Vbscript中循环后丢失值

Arrays 数组在Vbscript中循环后丢失值,arrays,loops,vbscript,Arrays,Loops,Vbscript,代码如下: 'creates the msxml object Set xmlDoc = CreateObject("Msxml2.DOMDocument.6.0") Dim retVal 'load the xml data of the script retVal=xmlDoc.load(argFilePath) Dim fso, folder, sFolder Dim xmlDataPath, curNode 'get input folder Set curNode=xmlDoc.

代码如下:

'creates the msxml object
Set xmlDoc = CreateObject("Msxml2.DOMDocument.6.0")
Dim retVal

'load the xml data of the script
retVal=xmlDoc.load(argFilePath)

Dim fso, folder, sFolder
Dim xmlDataPath, curNode

'get input folder
Set curNode=xmlDoc.selectSingleNode("//ScriptXmlData/inputFilePath")
Dim inputFolder, outputFolder, hotLoc
inputFolder=CSTR(curNode.text)

'location of jdf files
sFolder=inputFolder

'creating file getting object
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(sFolder)

Dim amount, fName, arrC, i
i=0

'loop for getting amounts
For each folderIdx In folder.files
    If fso.GetExtensionName(folderIdx.Name) = "jdf" Then

        'increase array size
        Redim arrC(i)

        'get folder name
        fName=folderIdx.Name

        'get file path
        xmlDataPath = sFolder+"\"+fName

        'load the xml data of the script
        retVal = jdfDoc.load(xmlDataPath)

        'get amount
        Set curNode = jdfDoc.selectSingleNode("//jdf:JDF/jdf:ResourceLinkPool/jdf:ComponentLink")
        amount = curNode.getAttribute("Amount")

        'Create array that holds amount
        arrC(i)=amount
        i=i+1

    End If
Next

wscript.echo arrC(0)
wscript.echo arrC(1)
wscript.echo arrC(2)
我们的问题是,一旦arrC退出循环,它的一些值就会丢失。 例如,在循环中,我们的数组是:

arrC(0)=100
arrC(1)=150
arrC(2)=200
一旦它离开循环,就像在代码末尾的测试中一样,它的值是:

arrC(0)=""
arrC(1)=""
arrC(2)=200
有人能解释一下吗


谢谢大家!

ReDim
在没有关键字
preserve
的情况下使用时不会保留数组的元素。换线

Redim arrC(i)
进入

顺便说一句,您不需要索引变量。当您将
arrC
初始化为循环外的空数组时,可以使用上界作为索引:

ReDim arrC(-1)

For Each folderIdx In folder.files
  If fso.GetExtensionName(folderIdx.Name) = "jdf" Then
    ...
    ReDim Preserve arrC(UBound(arrC)+1)
    arrC(UBound(arrC)) = amount
  End If
Next
但是,由于
ReDim Preserve
实际上创建了一个新数组并从旧数组复制了现有元素,因此随着数组大小的增加,它的性能必然会很差。您可能希望改用以下选项:


非常彻底!!非常感谢你,你太棒了!你能想出一种方法让变量发生这种情况吗?发生在我们之前,但我们能够避免它…什么类型的变量,在什么上下文中?我们创建了自己的“替换”函数,它在一个循环中。我们将结果字符串放入变量中。循环运行了3次,每次都替换了其他内容,然后在循环外变量将丢失其值可能是变量范围的问题。很难说没有看到实际的代码。
ReDim arrC(-1)

For Each folderIdx In folder.files
  If fso.GetExtensionName(folderIdx.Name) = "jdf" Then
    ...
    ReDim Preserve arrC(UBound(arrC)+1)
    arrC(UBound(arrC)) = amount
  End If
Next
Set arrC = CreateObject("System.Collections.ArrayList")

For Each folderIdx In folder.files
  If fso.GetExtensionName(folderIdx.Name) = "jdf" Then
    ...
    arrC.Add amount
  End If
Next