找回密码
 加入
搜索
查看: 2711|回复: 6

如何自定义函数使之输出为数组

  [复制链接]
发表于 2009-12-16 21:50:10 | 显示全部楼层 |阅读模式
本帖最后由 jchang 于 2009-12-17 21:11 编辑

由于如下类似的脚本要经常写:
$fileCICW=FileOpen(@ScriptDir&"\CICW.txt", 0)
If $fileCICW = -1 Then
    MsgBox(0, "Error", "Unable to open CICW.TXT.")
    Exit
EndIf
While 1
    $lineCICW = FileReadLine($fileCICW)
    If @error = -1 Then ExitLoop
        $arrayCICW=StringSplit($lineCICW,"=")
    FileWrite(@ScriptDir&"\CICW1.txt", $arrayCICW[1] & @CRLF)
    FileWrite(@ScriptDir&"\CICW2.txt", $arrayCICW[2] & @CRLF)
Wend
Dim $avArrayCICW1,$avArrayCICW2
_FileReadToArray ( @ScriptDir&"\CICW1.txt", $avArrayCICW1 )
_FileReadToArray ( @ScriptDir&"\CICW2.txt", $avArrayCICW2 )
FileDelete(@ScriptDir&"\CICW1.txt")
FileDelete(@ScriptDir&"\CICW2.txt")

以上脚本只与打开的文本CICW.txt有关,目的是想生成两个数组,$avArrayCICW1,$avArrayCICW2

想写一个函数实现以上的功能:
Func  STR($A,$B1,$B2)
        $fileA=FileOpen(@ScriptDir&"\"&$A, 0)
    If $fileA = -1 Then
    MsgBox(0, "Error", "Unable to open "$A)
    Exit
EndIf
While 1
    $lineA= FileReadLine($fileA)
    If @error = -1 Then ExitLoop
        $arrayA=StringSplit($lineA,"=")
    FileWrite(@ScriptDir&"\A1.txt", $arrayA[1] & @CRLF)
    FileWrite(@ScriptDir&"\A2.txt", $arrayA[2] & @CRLF)
Wend
Dim $B1,$B2
_FileReadToArray ( @ScriptDir&"\BOP1.txt", $B1 )
_FileReadToArray ( @ScriptDir&"\BOP2.txt", $B2 )
FileDelete(@ScriptDir&"\A1.txt")
FileDelete(@ScriptDir&"\A2.txt")       
EndFunc

但是在用的过程中确出错了,请问我的函数是否写法有误,我用该函数的时候输入的是:
STR("CICW.txt",$avArrayCICW1,$avArrayCICW2)

评分

参与人数 1金钱 +10 收起 理由
afan + 10 感谢主动将修改帖子分类为[已解决],请继续 ...

查看全部评分

 楼主| 发表于 2009-12-16 21:59:27 | 显示全部楼层
我觉得主要是我定义函数STR($A,$B1,$B2)的时候出的问题,不定义函数的脚本是可以正常运行的。
发表于 2009-12-17 13:22:22 | 显示全部楼层
回复 3# jchang


    Dim $B1,$B2 写在函数外面
 楼主| 发表于 2009-12-17 14:42:16 | 显示全部楼层
回复 4# afan
按照楼上的方法把脚本修改成了如下的形式:

Dim $avArrayBOP1,$avArrayBOP2
STR("BOP.txt",$avArrayBOP1,$avArrayBOP2)

Func  STR($A,$B1,$B2)
        $fileA=FileOpen(@ScriptDir&"\"&$A, 0)
    If $fileA = -1 Then
    MsgBox(0, "Error", "Unable to open "&$A)
    Exit
    EndIf
While 1
   $lineA= FileReadLine($fileA)
    If @error = -1 Then ExitLoop
        $arrayA=StringSplit($lineA,"=")
   FileWrite(@ScriptDir&"\A1.txt", $arrayA[1] & @CRLF)
   FileWrite(@ScriptDir&"\A2.txt", $arrayA[2] & @CRLF)
Wend
;Dim $B1,$B2
_FileReadToArray ( @ScriptDir&"\BOP1.txt", $B1 )
_FileReadToArray ( @ScriptDir&"\BOP2.txt", $B2 )

FileDelete(@ScriptDir&"\A1.txt")
FileDelete(@ScriptDir&"\A2.txt")
       
EndFunc

在运行后发现一闪就不见了,哪里还有问题呢?
发表于 2009-12-17 20:33:54 | 显示全部楼层
Func STR($A, ByRef $B1, ByRef $B2)
        $fileA = FileOpen(@ScriptDir & "" & $A, 0)
        If $fileA = -1 Then
                MsgBox(0, "Error", "Unable to open " $A)
                Exit
        EndIf
        While 1
                $lineA = FileReadLine($fileA)
                If @error = -1 Then ExitLoop
                $arrayA = StringSplit($lineA, "=")
                If @Error then ContinueLoop
                FileWrite(@ScriptDir & "\A1.txt", $arrayA[1] & @CRLF)
                FileWrite(@ScriptDir & "\A2.txt", $arrayA[2] & @CRLF)
        WEnd
        _FileReadToArray(@ScriptDir & "\A1.txt", $B1)
        _FileReadToArray(@ScriptDir & "\A2.txt", $B2)
        FileDelete(@ScriptDir & "\A1.txt")
        FileDelete(@ScriptDir & "\A2.txt")
EndFunc   ;==>STR
 楼主| 发表于 2009-12-17 21:11:05 | 显示全部楼层
正解,非常感谢
发表于 2015-10-17 11:51:14 | 显示全部楼层
标记一下,先
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-28 20:14 , Processed in 0.093174 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表