cashiba 发表于 2017-3-8 23:59:26

怎样把字符串转换为表达式(StringToExpression)

本帖最后由 cashiba 于 2017-3-12 00:49 编辑

color.txt01        [["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","","","","","",""]]color.Au3#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]
        _ArrayDisplay($crr)
next如上,文本中的字符串原本就是数组的赋值语句的形式
直接把字符串赋值给变量,该变量似乎不能成为数组.
怎样把"[[...,...,...]]"这种形式的字符串变成数组?

zghwelcome 发表于 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)

是不是这个意思

skysmile 发表于 2017-3-9 10:43:09

#include <Array.au3>
#include <File.au3>

Global $color,$ddd,$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 toUBound($color,1)-1
                        $ddd[$b][$i] = "0x" & $color[$b]
                Next
        Next
EndIf
_ArrayDisplay($ddd)

随便写看看效果吧

cashiba 发表于 2017-3-9 18:18:59

哦.还要用正则才行?
{:face (396):}
大概意思没说清楚.....
如下:#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)上面的数组赋值表达式代码运行后,可以得到数组:


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

lxwlxwayy 发表于 2017-3-10 10:34:18

申请数组变量就可以了

cashiba 发表于 2017-3-10 16:18:04

好像问题归结起来,就是字符串常量如何转变成字符串表达式的问题
如果有StringToExpression函数就好办了吧

cashiba 发表于 2017-3-10 16:19:43

在AU3里,是不是要用Assign-Eval什么的,不懂这个

shqf 发表于 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

cashiba 发表于 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列的数组
但是因为是从文本中取得的字符串常量形式,不是表达式,直接赋给变量却得不到数组
不知道怎么把字符串搞成表达式....

Duvet 发表于 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
        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

cashiba 发表于 2017-3-15 17:47:35

偷工減料的方法
Duvet 发表于 2017-3-15 13:54 http://www.autoitx.com/images/common/back.gif
挺好的阿,对这个问题来说能达到正确结果....
还是要用正则
如果有个普适性的StringToExpression函数就好了,能把字符串形式的表达式直接赋予表达式功能
页: [1]
查看完整版本: 怎样把字符串转换为表达式(StringToExpression)