找回密码
 加入
搜索
查看: 2991|回复: 10

[AU3基础] 怎样把字符串转换为表达式(StringToExpression)

  [复制链接]
发表于 2017-3-8 23:59:26 | 显示全部楼层 |阅读模式
本帖最后由 cashiba 于 2017-3-12 00:49 编辑

color.txt
01        [["0xFFFFCC","0xCCFFFF","0xFFCCCC","0xFFCCCC","0xFFFF99","0xCCCCFF","0xFF9966","0xFF6666","0xFFCCCC"],["0xFFCC99","0xCCFF99","0xCCCCCC","0xFFCCCC","0xCCCCFF","0xCCFFCC","0xCCFFFF","0xCCCCCC","0xCCFF99"],["0xFFCCCC","0xFFFFFF","0x99CC99","0x99CCCC","0xFFCC99","0xFFCCCC","0xCCCCFF","0xFFCCCC","0xCCFFFF"],["0xFFCC99","0xFFFFCC","0x99CCCC","","","","","",""]]
02        [["0xCCFF99","0xFFFFFF","0x99CCFF","0x99CCCC","0xFFFFFF","0xCCFF99","0xCCFFCC","0xFFFFFF","0x66CCCC"],["0xCCCCFF","0xFFFFFF","0x99CCCC","0xCCFFCC","0x99CCCC","0xFFFFCC","0xCCFFFF","0xFFFFFF","0xCCCCFF"],["0xCCFFFF","0xFFFFFF","0x99CCFF","0x66CCFF","0xFFFFFF","0xCCFFFF","0x6699CC","0xFFFFFF","0x99CCFF"],["0xCCCCFF","0xFFFFFF","0x99CCFF","","","","","",""]]
03        [["0x66CCCC","0xCCFF66","0xFF99CC","0xFF9999","0xFFFFFF","0xFFCC99","0xFF6666","0xFFFF66","0x99CC66"],["0x666699","0xFFFFFF","0xFF9999","0x99CC33","0xFF9900","0xFFCC00","0xFF0033","0xFFFFFF","0xFF9966"],["0xFF9900","0xCCFF00","0xCC3399","0x99CC33","0xFFFFFF","0xFF6600","0x993366","0xCCCC33","0x666633"],["0x66CCCC","0xFFFFFF","0x666699","","","","","",""]][code]color.Au3[code]#include <Array.au3>
#include <File.au3>

Local $Orr = 0
Local $file = @ScriptDir & "\Color.txt"
If Not _FileReadToArray($file, $Orr,4, @TAB) Then MsgBox(4096, "", "读取该文件时出错. @error: " & @error)
_ArrayDisplay($Orr)
for $i=0 to 2
        $crr=$Orr[$i][1]
        _ArrayDisplay($crr)
next
如上,文本中的字符串原本就是数组的赋值语句的形式
直接把字符串赋值给变量,该变量似乎不能成为数组.
怎样把"[[...,...,...]]"这种形式的字符串变成数组?

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2017-3-9 09:08:14 | 显示全部楼层
#include <Array.au3>
#include <File.au3>


Local $file = @ScriptDir & "\Color.txt"
$open = FileOpen($file,FileGetEncoding($file))
$read = FileRead($open)
FileClose($open)
$read_reg = StringRegExp($read,'"\[\[\S*\]\]"',3)
If Not @error Then _ArrayDisplay($read_reg)

是不是这个意思
发表于 2017-3-9 10:43:09 | 显示全部楼层
#include <Array.au3>
#include <File.au3>

Global $color,$ddd[36][3],$i

Local $file = @ScriptDir & "\Color.txt"
$open = FileOpen($file,FileGetEncoding($file))
$read = FileRead($open)
FileClose($open)
$read_reg = StringRegExp($read,'"\[\[\S*\]\]"',3)


If Not @error Then
        for $i=0 to UBound($read_reg,1)-1
                $color = StringRegExp($read_reg[$i],'""0x([^<>]+?)""',3)
                for $b = 0 to  UBound($color,1)-1
                        $ddd[$b][$i] = "0x" & $color[$b]
                Next
        Next
EndIf
_ArrayDisplay($ddd)

随便写看看效果吧
 楼主| 发表于 2017-3-9 18:18:59 | 显示全部楼层
哦.还要用正则才行?

大概意思没说清楚.....
如下:
#include <Array.au3>
local $Arr= [["0xFFFFCC","0xCCFFFF","0xFFCCCC","0xFFCCCC","0xFFFF99","0xCCCCFF","0xFF9966","0xFF6666","0xFFCCCC"],["0xFFCC99","0xCCFF99","0xCCCCCC","0xFFCCCC","0xCCCCFF","0xCCFFCC","0xCCFFFF","0xCCCCCC","0xCCFF99"],["0xFFCCCC","0xFFFFFF","0x99CC99","0x99CCCC","0xFFCC99","0xFFCCCC","0xCCCCFF","0xFFCCCC","0xCCFFFF"],["0xFFCC99","0xFFFFCC","0x99CCCC","","","","","",""]]
_ArrayDisplay($Arr)
上面的数组赋值表达式代码运行后,可以得到数组:


但是如顶楼那样,通过文本文件取得的类似形式的字符串,赋给变量后,这个变量却不是数组,似乎需要怎么转化一下才能成为数组

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2017-3-10 10:34:18 | 显示全部楼层
申请数组变量就可以了
 楼主| 发表于 2017-3-10 16:18:04 | 显示全部楼层
好像问题归结起来,就是字符串常量如何转变成字符串表达式的问题
如果有StringToExpression函数就好办了吧
 楼主| 发表于 2017-3-10 16:19:43 | 显示全部楼层
在AU3里,是不是要用Assign-Eval什么的,不懂这个
发表于 2017-3-11 09:37:49 | 显示全部楼层
#include <Array.au3>
#include <File.au3>
Local $Var = FileRead(@ScriptDir & "\Color.txt")
Local $Array = StringRegExp($Var, '\[\[(.+)\]\]', 3)
For $i = 0 To UBound($Array) - 1
        Local $nArray = StringSplit($Array[$i], "],[", 3)
        _ArrayDisplay($nArray)
Next
 楼主| 发表于 2017-3-11 21:09:06 | 显示全部楼层
本帖最后由 cashiba 于 2017-3-11 21:13 编辑

回复 8# shqf
你这段代码得到的数组不符合原形啊。
如下
[["0xFFFFCC","0xCCFFFF","0xFFCCCC","0xFFCCCC","0xFFFF99","0xCCCCFF","0xFF9966","0xFF6666","0xFFCCCC"],["0xFFCC99","0xCCFF99","0xCCCCCC","0xFFCCCC","0xCCCCFF","0xCCFFCC","0xCCFFFF","0xCCCCCC","0xCCFF99"],["0xFFCCCC","0xFFFFFF","0x99CC99","0x99CCCC","0xFFCC99","0xFFCCCC","0xCCCCFF","0xFFCCCC","0xCCFFFF"],["0xFFCC99","0xFFFFCC","0x99CCCC","","","","","",""]]
这本身就是数组的表达式
如果以表达式形式赋值给变量后,得到的是4行9列的数组
但是因为是从文本中取得的字符串常量形式,不是表达式,直接赋给变量却得不到数组
不知道怎么把字符串搞成表达式....
发表于 2017-3-15 13:54:23 | 显示全部楼层
偷工減料的方法

#include <Array.au3>

Local $read = _
                '01        [["0xFFFFCC","0xCCFFFF","0xFFCCCC","0xFFCCCC","0xFFFF99","0xCCCCFF","0xFF9966","0xFF6666","0xFFCCCC"],["0xFFCC99","0xCCFF99","0xCCCCCC","0xFFCCCC","0xCCCCFF","0xCCFFCC","0xCCFFFF","0xCCCCCC","0xCCFF99"],["0xFFCCCC","0xFFFFFF","0x99CC99","0x99CCCC","0xFFCC99","0xFFCCCC","0xCCCCFF","0xFFCCCC","0xCCFFFF"],["0xFFCC99","0xFFFFCC","0x99CCCC","","","","","",""]]' & @CRLF & _
                '02        [["0xCCFF99","0xFFFFFF","0x99CCFF","0x99CCCC","0xFFFFFF","0xCCFF99","0xCCFFCC","0xFFFFFF","0x66CCCC"],["0xCCCCFF","0xFFFFFF","0x99CCCC","0xCCFFCC","0x99CCCC","0xFFFFCC","0xCCFFFF","0xFFFFFF","0xCCCCFF"],["0xCCFFFF","0xFFFFFF","0x99CCFF","0x66CCFF","0xFFFFFF","0xCCFFFF","0x6699CC","0xFFFFFF","0x99CCFF"],["0xCCCCFF","0xFFFFFF","0x99CCFF","","","","","",""]]' & @CRLF & _
                '03        [["0x66CCCC","0xCCFF66","0xFF99CC","0xFF9999","0xFFFFFF","0xFFCC99","0xFF6666","0xFFFF66","0x99CC66"],["0x666699","0xFFFFFF","0xFF9999","0x99CC33","0xFF9900","0xFFCC00","0xFF0033","0xFFFFFF","0xFF9966"],["0xFF9900","0xCCFF00","0xCC3399","0x99CC33","0xFFFFFF","0xFF6600","0x993366","0xCCCC33","0x666633"],["0x66CCCC","0xFFFFFF","0x666699","","","","","",""]]'

Local $arrColor = StringToExpression($read)
_ArrayDisplay($arrColor)
Exit

Func StringToExpression(ByRef $strArray, $varName = "Array")
        Local $temp

        $temp = StringRegExp($strArray, '([\[,])(?|"([^"]*+)"|(''([^'']*+)'')|())(?=[,\]])', 3)
        If @error Then Return SetError(@error, @extended)

        Local $arrResult[1][1]
        Local $iRow = 0
        Local $iCol = 0
        Local $iMax = 1

        For $i = 0 To UBound($temp) - 1 Step 2
                Switch $temp[$i]
                        Case "["
                                $iRow += 1
                                $iCol = 1
                                ReDim $arrResult[$iRow][$iMax]
                        Case ","
                                $iMax = ($iCol > $iMax) ? $iCol : $iMax
                                ReDim $arrResult[$iRow][$iMax]
                        Case Else
                                ConsoleWrite("error: " & @error & @TAB & @extended & @TAB & $temp[$i + 1] & @CRLF)
                EndSwitch
                $arrResult[$iRow - 1][$iCol - 1] = $temp[$i + 1]
                $iCol += 1
        Next

        Assign($varName, $arrResult, 1)

        Return Eval($varName)
EndFunc   ;==>StringToExpression

评分

参与人数 1金钱 +31 收起 理由
cashiba + 31 非常感谢...

查看全部评分

 楼主| 发表于 2017-3-15 17:47:35 | 显示全部楼层
偷工減料的方法
Duvet 发表于 2017-3-15 13:54

挺好的阿,对这个问题来说能达到正确结果....
还是要用正则
如果有个普适性的StringToExpression函数就好了,能把字符串形式的表达式直接赋予表达式功能
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-11-21 21:30 , Processed in 0.086238 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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