rictirse 發表於 2012-10-21 16:02

AutoIt 幼幼班第二課 - 資料型態 與 字串處理

本帖最後由 rictirse 於 2012-12-2 14:56 編輯

簡述一下AutoIt 的資料型態

AutoIt 的資料型態要求沒有C語言那麼嚴謹

姑且不論壞處,好處就是簡單使用,讓一般用戶也可以輕易上手

常用的資料不外乎是(這邊我省略很多內容,有興趣請參考MSDN)

Variant (變數)
  $Var、$Time 這種內容可以隨時修改的通稱變數
String (字串)
  「我是字串」這就是一個字串的型態組合
Int (實數)
   1、2、3、4 這種沒有小數點的數字就是實數
Number(浮點數)
  0.01、3.1415 這些有小數點的通為 數字

資料型態轉換Dim $Str = "12"; 將資料型態宣告成為 String 格式
Dim $a = 10; 將資料型態宣告成為 Number 格式

Dim $b = $a + Int ($Str) ; 字串轉整數
MsgBox(0, "", $b)

Dim $s = String($a) & $Str ; 整數轉字串
MsgBox(0, "", $s)第一課學過 Array 陣列,Array 與 字串息息相關

譬如這句話「我是字串」其實就是用一維陣列高度4的陣列所組成

用程式碼來看的話Dim $Str, $Output=""

$Str = "我是字串"

;;; 函式介紹
;;; StringLen ( "字串" ) 返回指定字串的字元總數。
;;; StringMid ( "字串", 開始 [, 計數] ) 取某個字串的部分字元。


For $i = 1 To StringLen ($Str) ;; 創造一個迴圈 i 範圍為 1 到 StringLen (Str) 字串長度
      ;; 程式觀念是先將右邊計算完成後丟到左邊變數中
      ;; $Output = $Output & data 就等於 ($Output & data )兩者先結合 再把數據丟到左邊的 $Output中
      $Output = $Output & "String[" & $i & "]=" & StringMid ( $Str, $i, 1) & @CRLF
Next
MsgBox ( 64, "", $Output)


但是AutoIt 把字串陣列隱藏起來,方便用戶使用

所以宣告一個字串不需要給字串高度,就可以直接宣告

使用起來很方便,只是效能會比C略顯低落
StringInStr ( "字串","子字串" [, 區分大小寫 [, 順序 [, 開始 [, 計數]]]] )1. #cs2. 1. 先用 StringInStr 找到 pos3. 2. 取 Str 左邊 pos-1 個,轉整數4. 3. 取 Str 右邊 pos+1 個,再丟回給 Str5.6. #ce7. ; --------------------------------8. Dim $Str, $ret=""9. Dim $Msg=""10.Dim $Array11.Dim $Pos=012.Dim $Cnt=013. 14.; --------------------------------15.$Str = InputBox ("輸入","","1,2,3,4,5,6")16.For $i=0 to 517.      $Pos = StringInStr($Str,",") ; 找第一個出現 , 之位置18.      $Array[$i] = Int ( StringLeft ( $Str, $Pos-1) ) ;; 第一個 pos 位置-1 就是 數字的位置19.      $Str = StringMid($Str, $Pos+1); 取剩下的字串20.      $Cnt+=1;計數器+121.Next22.$Array = Int($Str); 最後一個陣列為 剩下的字串23. 24.; --------------------------------25.For $i=0 To $Cnt-126.      $Msg = $Msg & $Array[$i] &@CRLF27.Next28.MsgBox(64, "", $Msg)StringSplit ( "字串","分隔符號" [, 旗標 ] )1. Dim $Msg2.3. $Str = InputBox ("輸入","","1,2,3,4,5,6");設定 $Str = "1,2,3,4,5,6"4. $ret = StringSplit ( $Str, ',');分割$Str 字串, 分割符號為 「,」5. ;;; $ret = 返回分割數量6. For $i = 1 to $ret7.         $Msg = $Msg & $ret[$i]&@CRLF8. Next9. MsgBox( 64, "", $Msg)10. 11.; --------------------------------12.$Msg = "" ;重設msg = 空白13. 14.$Str = InputBox ("輸入","","1,2,3,4,5,6")15.$ret = StringSplit ( $Str, ',', 2) ;StringSplit ( $Str, ',', 2) = 2 則不會返回 $ret 則不會返回高度16. 17.For $i = 0 to UBound ($ret)-1;因為不會返回高度 所以是0 to (UBound ($ret)-1)18.      $Msg = $Msg & $ret[$i]&@CRLF19.Next20.MsgBox( 64, "", $Msg)StringFormat ( "格式控制",變數1 [, ... 變數32 ] )2.; ------------------------------------------------------
3.; 變數宣告
4.Dim $Int = 10
5.Dim $i1=1, $i2=2, $i3=3, $i4=4
6.Dim $f = 3.1415926
7.Dim $s1, $s2, $s3,$s4,$s5, $s6
8.Dim $Msg;
9.#cs
10.; ------------------------------------------------------
11.; %d : 整數
12.$Msg = $i1 & "," & $i2 & "," & $i3
13.MsgBox(64, "$Msg=", $Msg)
14.
15.$Msg = StringFormat("%d,%d,%d",$I1,$I2,$I3)
16.MsgBox(64, "simple stringformat", $Msg)
17.
18.; ------------------------------------------------------
19.; 特殊符號之表達
20.; \n , \r\n : @LF, @CRLF
21.; \\      : \
22.; \"      : "
23.; \'      : '
24.; \t      : Tab
25.; \a      ; beep (in console)
26.
27.$Msg = StringFormat("%d\\%d\\%d\n",$I1,$I2,$I3)
28.MsgBox(64, "escape character demo", $Msg)
29.#ce
30.; ------------------------------------------------------
31.; 進制
32.
33.$Msg = StringFormat("%%d=%d\n%%u=%u\n%%o=%o\n%%x=%x\n%%X=%X\n", _
34.                                           $Int,$Int,$Int,$Int,$Int)
35.MsgBox(64, "進制 1 ",$Msg)
36.
37.$Msg = StringFormat("%%d=%d\n%%u=%u\n%%o=0%o\n%%x=0x%x\n%%X=0X%X\n", _
38.                                           $Int,$Int,$Int,$Int,$Int)
39.MsgBox(64, "進制 1 ",$Msg)
40.
41.$Msg = StringFormat("%%#d=%#d\n%%#u=%#u\n%%#o=%#o\n%%#x=%#x\n%%#X=%#X\n", _
42.                                           $Int,$Int,$Int,$Int,$Int)
43.MsgBox(64, "進制 2 ",$Msg)
44.
45.
46.#cs
47.; ------------------------------------------------------
48.; %d : 整數
49.
50.$s1 = StringFormat("Integer: [%d]",$Int)
51.$s2 = StringFormat("Integer: [%5d]",$Int); 欄位長度 5
52.$s3 = StringFormat("Integer: [%-5d]",$Int) ; 左對齊
53.$s4 = StringFormat("Integer: [% 5d]",$Int) ; 保留符號欄位
54.$s5 = StringFormat("Integer: [%+5d]",$Int) ; 顯示 +- 號
55.$s6 = StringFormat("Integer: [%05d]",$Int) ; 填0
56.$Msg = StringFormat("s1=%s\ns2=%s\ns3=%s\ns4=%s\ns5=%s\ns6=%s\n", $s1,$s2,$s3,$s4,$s5, $s6)
57.MsgBox(64, "%d demo", $Msg)
58.
59.; ------------------------------------------------------
60.; %s : 字串
61.
62.$s1 = "hello"
63.$s2 = StringFormat("string s1 = [%s]\n",    $s1)
64.$s3 = StringFormat("string s1 = [%10s]\n",$s1); 寬度10
65.$s4 = StringFormat("string s1 = [%-10s]\n", $s1); 寬度 10 靠左
66.$s5 = StringFormat("string s1 = [%010s]\n", $s1); 寬度 10 補 0
67.$Msg = $s1 & $s2 & $s3 & $s4 & $s5
68.MsgBox(64, "%s demo", $Msg)
69.#ce
70.; ------------------------------------------------------
71.;~ ; %f : 浮點數- part 1
72.$s1 = StringFormat("%%f      = [%f]\n", $f)         ; 四舍五入至第六位   
73.$s2 = StringFormat("%%10f    = [%10f]\n", $f)       ; 寬度為 10
74.$s3 = StringFormat("%%.2f    = [%.2f]\n", $f)       ; 顯示小數點二位
75.$s4 = StringFormat("%%10.2f= [%10.2f]\n", $f)   ; 整體寬度10(含小數點),小數點二位
76.$s5 = StringFormat("%%-10.2f = [%-10.2f]\n", $f)   ; 整體寬度10(含小數點),小數點二位, 靠左對齊
77.
78.$Msg = $s1 & $s2 & $s3 & $s4 & $s5
79.MsgBox(64, "%f demo - 1", $Msg)
80.
81.; ------------------------------------------------------
82.;~ ; %f : 浮點數- part 2
83.$s1 = StringFormat("%%f       = [%+f]\n", $f)          ; 四舍五入至第六位   
84.$s2 = StringFormat("%%+10.2f= [%+10.2f]\n", $f)      ; 整體寬度10(含小數點),小數點二位, 顯示正負號
85.$s3 = StringFormat("%%-+10.2f = [%-+10.2f]\n", $f)   ; 整體寬度10(含小數點),小數點二位, 靠左對齊, error
86.$s4 = StringFormat("%% -10.2f = [% -10.2f]\n", $f)   ; 整體寬度10(含小數點),小數點二位, 靠左對齊, error
87.
88.$Msg = $s1 & $s2 & $s3 & $s4
89.MsgBox(64, "%f demo - 2", $Msg)
90.
91.
92.; ------------------------------------------------------
93.;~ ; %g: 浮點數- part 3
94.$f = 3.141591415914159                     ; 再觀查 3.1
95.;$f = 3.1
96.$s1= StringFormat("%%g = [%g]\n", $f)      ; 自動調整, 最長到小數後五位
97.$s2= StringFormat("%%g = [%.10g]\n", $f)   ; 自動調整, 最長共 10 位
98.$s3= StringFormat("%%e = [%e]\n", $f)      ; 科學符號表示法, 預設 5 位
99.$s3= StringFormat("%%e = [%.15E]\n", $f)   ; 科學符號表示法, 顯示15位小數
100.
101.$Msg = $s1 & $s2 & $s3
102.MsgBox(64, "floating demo", $Msg)

gary8349 發表於 2013-2-15 17:02

本帖最後由 gary8349 於 2013-2-15 17:08 編輯

~~~補上遺失的樓層~~~

接著教 字串處理的基本語法使用

StringInStr ( "字串", "子字串" [, 區分大小寫 [, 順序 [, 開始 [, 計數]]]] )#cs
    1. 先用 StringInStr 找到 pos
    2. 取 Str 左邊 pos-1 個,轉整數
    3. 取 Str 右邊 pos+1 個,再丟回給 Str
   
   #ce
   ; --------------------------------
   Dim $Str, $ret=""
   Dim $Msg=""
   Dim $Array
   Dim $Pos=0
   Dim $Cnt=0
   
   ; --------------------------------
   $Str = InputBox ("輸入","","1,2,3,4,5,6")
   For $i=0 to 5
         $Pos = StringInStr($Str, ",") ; 找第一個出現 , 之位置
         $Array[$i] = Int ( StringLeft ( $Str , $Pos-1) ) ;; 第一個 pos 位置-1 就是 數字的位置
         $Str = StringMid($Str, $Pos+1); 取剩下的字串
         $Cnt+=1;計數器+1
   Next
   $Array = Int($Str); 最後一個陣列為 剩下的字串
   
   ; --------------------------------
   For $i=0 To $Cnt-1
         $Msg = $Msg & $Array[$i] & @CRLF
   Next
   MsgBox(64, "", $Msg)

StringSplit ( "字串", "分隔符號" [, 旗標 ] )Dim $Msg

   $Str = InputBox ("輸入","","1,2,3,4,5,6"); 設定 $Str = "1,2,3,4,5,6"
   $ret = StringSplit ( $Str, ',');分割$Str 字串, 分割符號為 「,」
   ;;; $ret = 返回分割數量
   For $i = 1 to $ret
         $Msg = $Msg & $ret[$i] &@CRLF
   Next
   MsgBox( 64, "", $Msg)
   
   ; --------------------------------
   $Msg = "" ;重設msg = 空白
   
   $Str = InputBox ("輸入","","1,2,3,4,5,6")
   $ret = StringSplit ( $Str, ',', 2) ;StringSplit ( $Str, ',', 2) = 2 則不會返回 $ret 則不會返回高度
   
   For $i = 0 to UBound ($ret)-1;因為不會返回高度 所以是0 to (UBound ($ret)-1)
         $Msg = $Msg & $ret[$i] &@CRLF
.   Next
   MsgBox( 64, "", $Msg)

StringFormat ( "格式控制", 變數1 [, ... 變數32 ] ); ------------------------------------------------------
   ; 變數宣告
   Dim $Int = 10
   Dim $i1=1, $i2=2, $i3=3, $i4=4
   Dim $f = 3.1415926
   Dim $s1, $s2, $s3,$s4,$s5, $s6
   Dim $Msg;
   #cs
   ; ------------------------------------------------------
   ; %d : 整數
   $Msg = $i1 & "," & $i2 & "," & $i3
   MsgBox(64, "$Msg=", $Msg)
   
   $Msg = StringFormat("%d,%d,%d",$I1,$I2,$I3)
   MsgBox(64, "simple stringformat", $Msg)
   
   ; ------------------------------------------------------
   ; 特殊符號之表達
   ; \n , \r\n : @LF, @CRLF
   ; \\      : \
   ; \"      : "
   ; \'      : '
   ; \t      : Tab
   ; \a      ; beep (in console)
   
   $Msg = StringFormat("%d\\%d\\%d\n",$I1,$I2,$I3)
   MsgBox(64, "escape character demo", $Msg)
   #ce
   ; ------------------------------------------------------
   ; 進制
   
   $Msg = StringFormat("%%d=%d\n%%u=%u\n%%o=%o\n%%x=%x\n%%X=%X\n", _
                                          $Int,$Int,$Int,$Int,$Int)
.   MsgBox(64, "進制 1 ",$Msg)
   
   $Msg = StringFormat("%%d=%d\n%%u=%u\n%%o=0%o\n%%x=0x%x\n%%X=0X%X\n", _
                                          $Int,$Int,$Int,$Int,$Int)
   MsgBox(64, "進制 1 ",$Msg)
   
   $Msg = StringFormat("%%#d=%#d\n%%#u=%#u\n%%#o=%#o\n%%#x=%#x\n%%#X=%#X\n", _
                                          $Int,$Int,$Int,$Int,$Int)
   MsgBox(64, "進制 2 ",$Msg)
   

   #cs
   ; ------------------------------------------------------
   ; %d : 整數
   
   $s1 = StringFormat("Integer: [%d]",$Int)
   $s2 = StringFormat("Integer: [%5d]",$Int); 欄位長度 5
   $s3 = StringFormat("Integer: [%-5d]",$Int) ; 左對齊
.   $s4 = StringFormat("Integer: [% 5d]",$Int) ; 保留符號欄位
   $s5 = StringFormat("Integer: [%+5d]",$Int) ; 顯示 +- 號
   $s6 = StringFormat("Integer: [%05d]",$Int) ; 填0
   $Msg = StringFormat("s1=%s\ns2=%s\ns3=%s\ns4=%s\ns5=%s\ns6=%s\n", $s1,$s2,$s3,$s4,$s5, $s6)
   MsgBox(64, "%d demo", $Msg)
   
   ; ------------------------------------------------------
   ; %s : 字串
   
   $s1 = "hello"
   $s2 = StringFormat("string s1 = [%s]\n",    $s1)
   $s3 = StringFormat("string s1 = [%10s]\n",$s1); 寬度10
   $s4 = StringFormat("string s1 = [%-10s]\n", $s1); 寬度 10 靠左
   $s5 = StringFormat("string s1 = [%010s]\n", $s1); 寬度 10 補 0
   $Msg = $s1 & $s2 & $s3 & $s4 & $s5
   MsgBox(64, "%s demo", $Msg)
   #ce
.   ; ------------------------------------------------------
   ;~ ; %f : 浮點數- part 1
   $s1 = StringFormat("%%f      = [%f]\n", $f)         ; 四舍五入至第六位   
   $s2 = StringFormat("%%10f    = [%10f]\n", $f)       ; 寬度為 10
.   $s3 = StringFormat("%%.2f    = [%.2f]\n", $f)       ; 顯示小數點二位
   $s4 = StringFormat("%%10.2f= [%10.2f]\n", $f)   ; 整體寬度10(含小數點),小數點二位
   $s5 = StringFormat("%%-10.2f = [%-10.2f]\n", $f)   ; 整體寬度10(含小數點),小數點二位, 靠左對齊
   
   $Msg = $s1 & $s2 & $s3 & $s4 & $s5
   MsgBox(64, "%f demo - 1", $Msg)
   
   ; ------------------------------------------------------
   ;~ ; %f : 浮點數- part 2
   $s1 = StringFormat("%%f       = [%+f]\n", $f)          ; 四舍五入至第六位   
   $s2 = StringFormat("%%+10.2f= [%+10.2f]\n", $f)      ; 整體寬度10(含小數點),小數點二位, 顯示正負號
   $s3 = StringFormat("%%-+10.2f = [%-+10.2f]\n", $f)   ; 整體寬度10(含小數點),小數點二位, 靠左對齊, error
   $s4 = StringFormat("%% -10.2f = [% -10.2f]\n", $f)   ; 整體寬度10(含小數點),小數點二位, 靠左對齊, error
   
   $Msg = $s1 & $s2 & $s3 & $s4
   MsgBox(64, "%f demo - 2", $Msg)
.   
   
   ; ------------------------------------------------------
   ;~ ; %g: 浮點數- part 3
   $f = 3.141591415914159                     ; 再觀查 3.1
   ;$f = 3.1
   $s1= StringFormat("%%g = [%g]\n", $f)      ; 自動調整, 最長到小數後五位
   $s2= StringFormat("%%g = [%.10g]\n", $f)   ; 自動調整, 最長共 10 位
   $s3= StringFormat("%%e = [%e]\n", $f)      ; 科學符號表示法, 預設 5 位
   $s3= StringFormat("%%e = [%.15E]\n", $f)   ; 科學符號表示法, 顯示15位小數
   
   $Msg = $s1 & $s2 & $s3
.   MsgBox(64, "floating demo", $Msg)
.再來教一下很重要的必要知識 Function 副程式

1.   什麼時候需要用到Function 呢? 一個動作需要重複做n次這時候用Function 就相當好用

Function寫完畢後整個程式的架構會很容易看懂,而且Function非常容易維護

拿SWAP 當例子

SWAP 功能很簡單就是,要把兩個資料互換就需要 SWAP

原理

定義A = 5, B = 10

兩者間資料要互換當然需要第三者一個暫存資料區

A = tmp; 先將 A數值丟給 tmp

B = A; 把B的數值丟給A

B = tmp; 再把A暫存在tmp的數值丟給B

就完成了交換的動作

至於要怎麼包成 Function呢?; ----------------
   ; declare variable
   Dim $a, $b
   
   ; ----------------
   $x=5
   $y=10
   Swap1($x,$y)
   MsgBox(0, "after swap 1", StringFormat("a=%d\nb=%d\n", $x, $y))
   ; ----------------
   $x=5
.   $y=10
   Swap2($x,$y)
   MsgBox(0, "after swap 2", StringFormat("a=%d\nb=%d\n", $x, $y))
   
   ; ----------------
   Func Swap1($a, $b)
         Local $tmp = $a
         $a = $b
         $b = $tmp
   EndFunc
   ; ----------------
   Func Swap2(ByRef $a,ByRef $b)
         Local $tmp = $a
         $a = $b
         $b = $tmp
   EndFunc
Swap1為什麼沒把資料交換過去呢?

因為 Swap1只有把數值丟進 Function 交換

交換後並沒有再把 數值丟出來

Swap2(ByRef $a,ByRef $b)

ByRef 就是把運算結果再返回 $a $b 這就是我們要的結果

接著再來練習一題

當輸入一個一維陣列的資料,返回陣列內全部的內容Dim $data = ;定義陣列高度與初始值
   Display($data);呼叫 Function
   
   ; ------------------------------
   Func Display (Const $data);定義data 為常數不得修改
         Local $Msg = "";定義 Msg 初始值為空白
         For $i=0 to UBound ($data)-1
                   $Msg = $Msg & StringFormat ( "data[%d] = %d\n", $i, $data[$i])
         Next
         MsgBox(0, "Display data", $Msg)
   EndFunc

接著練習輸入一個陣列後返回陣列內最大的數字位置(POS)Dim $data = ;定義 一維陣列 高度10 並設定初始值
   
$pos = FindMaxIndex ($data); 呼叫Function

   MsgBox ( 0, "", $pos)
   
   Func FindMaxIndex ( Const $arr, Const $Start_Pos = 0);; $Start_Pos = 0 意思是 預設值 等於0
         Local $Pos = $Start_Pos; 設定初始位置預設 = 0
         ;;; i 的範圍 = 初始位置($Pos) 到 陣列最高點-1 (UBound ($arr)-1)
         For $i= ($Pos+1) To (UBound ($arr)-1)
                   If ($arr[$i] > $arr[$Pos]) Then ; 如果 目前陣列數值 > 目前陣列最大值
                           $Pos = $i ; 把目前的位置 傳送給 Pos 當成最大值位置
                   EndIf
         Next
   
         Return $Pos;; 返回最大值
   EndFunc


接著用 上面程式的思路寫一隻 傳遞最小數值位置的Function
答案↓
FuncFindMinIndex ( Const $arr, Const $Start_Pos = 0)
      Local $Pos = $Start_Pos


      For $i=$Pos+1 To UBound ($arr)-1
                If ($arr[$i]< $arr[$Pos]) Then
                     $Pos=$i
                EndIf
      Next


      Return $Pos
EndFunc

接著詳細介紹一下 Function 返回資料
沒使用 ByRef#include <Array.au3>
   
   Global $Array ;; 全域宣告

   _ArrayDisplay ($Array, "_ArrAdd前的display");; 顯示空的 array 內容
   _ArrAdd ($Array)
   _ArrayDisplay ($Array, "_ArrAdd後的display") ;; 經過_ArrAdd後 內容還是空的 Function 並沒有返回資料

   Func _ArrAdd($Array)
         For $i=0 To UBound ($Array)-1
                   $Array[$i]+=1
         Next
         _ArrayDisplay ($Array, "Function內的display");; 顯示 _ArrAdd 後的內容
   EndFunc


使用 ByRef#include <Array.au3>

   Global $Array ;; 全域宣告
   
   _ArrayDisplay ($Array, "_ArrAdd前的display");; 顯示空的 array 內容
   _ArrAdd ($Array)
   _ArrayDisplay ($Array, "_ArrAdd後的display") ;; 經過_ArrAdd 後的內容
   
   Func _ArrAdd(ByRef $Array);; 加上 ByRef返回陣列內的資料
         For $i=0 To UBound ($Array)-1
                   $Array[$i]+=1
         Next
         _ArrayDisplay ($Array, "Function內的display");; 顯示 _ArrAdd 後的內容
   EndFunc

使用 Return,但並未宣告變數繼承Return回來的資料#include <Array.au3>
   
   Global $Array ;; 全域宣告

   _ArrayDisplay ($Array, "_ArrAdd前的display");; 顯示空的 array 內容
   _ArrAdd ($Array)
   _ArrayDisplay ($Array, "_ArrAdd後的display") ;; 經過_ArrAdd 後的內容,但並無法顯示 下一個例題會講解為什麼無法顯示
   
   Func _ArrAdd($Array)
         For $i=0 To UBound ($Array)-1
                   $Array[$i]+=1
         Next
         _ArrayDisplay ($Array, "Function內的display");; 顯示 _ArrAdd 後的內容
         Return $Array ;; 使用 Return 返回資料
   EndFunc


宣告變數繼承Return回來的資料#include <Array.au3>

   Global $Array ;; 全域宣告

   _ArrayDisplay ($Array, "_ArrAdd前的display");; 顯示空的 array 內容
   $iArray = _ArrAdd ($Array) ;;; 宣告 $iArray =_ArrAdd ($Array) 返回的資料
   _ArrayDisplay ($iArray, "_ArrAdd後的display") ;; 經過_ArrAdd 後的內容
   
   Func _ArrAdd($Array)
         For $i=0 To UBound ($Array)-1
                   $Array[$i]+=1
         Next
         _ArrayDisplay ($Array, "Function內的display");; 顯示 _ArrAdd 後的內容
         Return $Array ;; 使用 Return 返回資料
   EndFunc


接著介紹 InsertSorting 需要把前面練習的 Display、Swap、FindMinIndexor FindMaxIndex 綜合練習題

思路是這樣的請參考ppt

Dim $data =
   Display($data);; 置換前
   InsertSort($data)
   Display($data);; 置換後
   
   ; ------------------------------
   Func InsertSort (ByRef $data)
         Local $Pos
         For $i=0 to UBound($data)-2
                   $Pos=$i
                   For $j=$i+1 to UBound($data)-1
                           If $data[$Pos] > $data[$j] Then
                                 $Pos = $j
                           EndIf
                   Next
                   If $Pos <> $i Then
                           Swap( $data[$Pos], $data[$i])
                   EndIf
         Next
   EndFunc
   
   ; ------------------------------
   ; swap function
   Func Swap(ByRef $a,ByRef $b)
         Local $Tmp = $a
         $a = $b
         $b = $Tmp
   EndFunc
   
   ; ------------------------------
   ; Display function
   Func Display(const $data)
         Local $Msg=""
         For $i=0 to UBound($data)-1
                   $Msg = $Msg & StringFormat("data[%d] = %d\n", $i, $data[$i])
         Next
         MsgBox(0, "Display data", $Msg)
   EndFunc
頁: [1]
查看完整版本: AutoIt 幼幼班第二課 - 資料型態 與 字串處理