判断INI里的关键字是否达到设置匹配数[已解决]
本帖最后由 dunphy 于 2014-11-27 19:22 编辑abcd1234在前面发了一贴,我根据他的思路整理了一下,附件跟代码附上,如何解决以下代码的问题?
需要解决的是:
按下一次按键 判定关键字BB=1的情况,如果BB=1的数量不足设置的匹配数,则写入值BB=1直到按N次按键后达到这个匹配数;如果BB=1的数量达到了这个比配数就接着判定BB=2的情况。。。 .一直判定到BB=N。。。
(我这里暂时设定了五种BB的情况)
aa.ini
aa=5416
bb=1
aa=1814
bb=1
aa=3097
bb=1
aa=1818
bb=1
aa=6171
bb=1
aa=4922
bb=2
aa=2199
bb=2
aa=6919
bb=2
aa=2589
bb=2
aa=6656
bb=2
aa=3676
bb=2
aa=7727
bb=3
aa=6269
bb=3
aa=5196
bb=3
aa=3516
bb=3
aa=5280
bb=3
aa=6483
bb=3
aa=7071
bb=3
aa=3870
bb=4
aa=1504
bb=4
aa=1795
bb=4
aa=3075
bb=4
aa=1630
bb=4
aa=2511
bb=4
aa=2295
bb=5
aa=5160
bb=5
aa=5859
bb=5
aa=1572
bb=5
aa=3026
bb=5
aa=4226
bb=5
aa=4176
bb=5
aa=7024
bb=5
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiIPAddress.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <Array.au3>
HotKeySet("{ESC}", "HotKeyPressed")
Global Const $gc_formName = "配置文件"
Global $ini = @ScriptDir & "\*.ini"
Global $a_PznameA = "aa", $a_PznameB = "bb"
Local $a_bbval, $P = 1
For $i = 1 To 5
$a_bbval[$i] = $P
$P += 1
Next
#Region
Global $gu_PzForm = GUICreate("gc_formName", 403, 289, 340, 128)
Global $gu_PzGroup1 = GUICtrlCreateGroup(" 设置随机数", 16, 8, 377, 49)
Global $gu_PzInput1 = GUICtrlCreateInput("", 39, 23, 41, 21, $ES_READONLY)
Global $gu_PzLabel1 = GUICtrlCreateLabel("个随机数", 84, 30, 52, 17)
Global $gu_PzButton1 = GUICtrlCreateButton("设置", 304, 24, 51, 25)
GUICtrlCreateGroup("", -99, -99, 1, 1)
Global $gu_PzGroup2 = GUICtrlCreateGroup("设置配置文件", 13, 62, 382, 68)
Global $gu_PzInput2 = GUICtrlCreateInput("", 33, 88, 244, 21)
GUICtrlSetData($gu_PzInput2, $ini)
Global $gu_PzButton2 = GUICtrlCreateButton("打开文件", 296, 86, 70, 25)
GUICtrlCreateGroup("", -99, -99, 1, 1)
Global $gu_PzGroup3 = GUICtrlCreateGroup(" 设置匹配值", 16, 136, 377, 57)
Global $gu_PzInput3 = GUICtrlCreateInput("", 36, 157, 49, 21)
GUICtrlCreateGroup("", -99, -99, 1, 1)
Global $gu_PzButton3 = GUICtrlCreateButton("关于(A)", 13, 250, 59, 25)
Global $gu_PzButton4 = GUICtrlCreateButton("判断", 264, 250, 59, 25)
Global $gu_PzButton5 = GUICtrlCreateButton("退出(X)", 328, 250, 59, 25)
GUISetState(@SW_SHOW)
#EndRegion
_Main() ;主函数开始
Exit
Func _Main()
Local $nMsg
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $gu_PzButton1
;Local $Number = Random(1, 29, 1)
GUICtrlSetData($gu_PzInput1, Random(150, 200, 1))
Local $pz_rdnum = GUICtrlRead($gu_PzInput1)
MsgBox(0, "提示", "准备输入" & $pz_rdnum & "个随机数", 1)
Case $gu_PzButton2
If GUICtrlRead($gu_PzInput1) = "" Then
MsgBox(16, "提示", "请先设置随机数")
Else
Local $path = FileOpenDialog("配置文件存储位置", _
@ScriptDir, "配置文件.ini (*.ini)|所有文件(*.*)", 1 + 8, _
"*.ini", $gu_PzForm)
If @error Then
MsgBox(16, "提示", "你未选择INI文件.")
Else
Global $a_Numbers = _RandomNumber()
; _SortArray($a_Numbers) ;从小到大排序
GUICtrlSetData($gu_PzInput2, $path)
EndIf
EndIf
Case $gu_PzButton4
If GUICtrlRead($gu_PzInput1) = "" Then
MsgBox(16, "提示", "请先设置随机数再判断")
Else
Local $sSection = IniReadSectionNames( GUICtrlRead($gu_PzInput2))
;_ArrayDisplay($sSection, "$sSection")
If @error Then
MsgBox(16, "提示", "先配置文件地址再判断")
Else
Global $a_SecName[$sSection + 1]
For $i = 1 To UBound($a_SecName) - 1
Local $pz_array = IniReadSection($path, $sSection[$i])
$a_SecName = "字段名"
$a_SecName = "第一关键字"
$a_SecName = "第一关键字的值"
$a_SecName = "第二关键字"
$a_SecName = "第二关键字的值"
$a_SecName[$i] = $sSection[$i]
$a_SecName[$i] = $pz_array
$a_SecName[$i] = $pz_array
$a_SecName[$i] = $pz_array
$a_SecName[$i] = $pz_array
Next
;; _ArrayDisplay($a_SecName, "$Array")
_Exists()
EndIf
EndIf
Case $gu_PzButton3
MsgBox(0 + 64, $gc_formName, "提示:" & @CRLF & "1.先设置随机数" _
& @CR & "2.打开文件" _
& @CR & "3.设置查寻值并判断。")
Case $gu_PzButton5
Exit
EndSwitch
WEnd
EndFunc ;==>_Main
Func _RandomNumber()
Local $a_Numbers, $P = 1
While $P <=GUICtrlRead($gu_PzInput1) ;随机 所设的上限值 个四位正整数
Local $Number = StringFormat("%03s", Random(1000, 8000, 1))
Local $i, $f = 1
For $i = 1 To UBound($a_Numbers, 1) - 1
If $a_Numbers[$i] = $Number Then
$f = 0
ExitLoop
EndIf
Next
If $f = 1 Then
ReDim $a_Numbers[$P + 1]
$a_Numbers[$P] = $Number
$P += 1
EndIf
WEnd
;; _ArrayDisplay( $a_Numbers," $a_Numbers")
Return $a_Numbers
EndFunc ;==>_RandomNumber
Func _Exists()
Local $SearchVal = GUICtrlRead($gu_PzInput3)
If $SearchVal = "" Then
MsgBox(16, "提示", "请输入查寻值", 1)
ElseIf StringIsInt($SearchVal) = "" Then
MsgBox(16, "提示", "你输入查寻值为" & $SearchVal & @CRLF & "你输入的值不为数字请重新输入")
Else
Local $i=1, $flag=1
Local $iIndex = _ArrayFindAll($a_SecName, $a_bbval[$i], Default, Default, Default, Default, 4)
TrayTip("现在有" & UBound($iIndex) & "个BB=" & $a_bbval[$i] & @CRLF & ",你的匹配值为" & $SearchVal, "提示",1)
Local $j = Random(100, GUICtrlRead($gu_PzInput1), 1)
Local $k = Random(100, GUICtrlRead($gu_PzInput1), 1)
If UBound($iIndex) < $SearchVal Then ;小于所设的匹配值则写入一个BB=1,否则
IniWrite("aa.ini", $a_Numbers[$j], $a_PznameA, $a_Numbers[$k])
IniWrite("aa.ini", $a_Numbers[$j], $a_PznameB, $a_bbval[$i])
Else
; MsgBox(0, "提示", "BB值的数量等于所设的匹配值",1)
$I+=1
; If UBound($iIndex) = $SearchValThen ;等于或者大于所设的匹配值就判断BB值+1的情况
Local $iIndex = _ArrayFindAll($a_SecName, $a_bbval[$i], Default, Default, Default, Default, 4)
TrayTip("现在有" & UBound($iIndex) & "个BB=" & $a_bbval[$i] & @CRLF & ",你的匹配值为" & $SearchVal, "提示",1)
Local $j = Random(100,GUICtrlRead($gu_PzInput1), 1)
Local $k = Random(100, GUICtrlRead($gu_PzInput1), 1)
IniWrite("aa.ini", $a_Numbers[$j], $a_PznameA, $a_Numbers[$k])
IniWrite("aa.ini", $a_Numbers[$j], $a_PznameB, $a_bbval[$i])
; EndIf
EndIf
_ArrayDisplay($a_SecName, "$Array")
EndIf
EndFunc ;==>_Exists
Func HotKeyPressed()
Switch @HotKeyPressed ; 最后按下的热键.
Case "{ESC}" ; {ESC} 是"退出"热键.
Exit
EndSwitch
EndFunc ;==>HotKeyPressed
本帖最后由 dunphy 于 2014-11-25 03:37 编辑
如果像这样的BB的值很多的情况下如何办??已经写成数组了。如何来循环??
其中AA的值 不固定
【段名】 也不固定
比方我所设的比配数是8,我所要达到的效果是下面的配置文件
aa.ini (经过判断后的AA.INI)
aa=5416
bb=1
aa=1814
bb=1
aa=3097
bb=1
aa=1818
bb=1
aa=6171
bb=1
aa=4922
bb=2
aa=2199
bb=2
aa=6919
bb=2
aa=2589
bb=2
aa=6656
bb=2
aa=3676
bb=2
aa=7727
bb=3
aa=6269
bb=3
aa=5196
bb=3
aa=3516
bb=3
aa=5280
bb=3
aa=6483
bb=3
aa=7071
bb=3
aa=3870
bb=4
aa=1504
bb=4
aa=1795
bb=4
aa=3075
bb=4
aa=1630
bb=4
aa=2511
bb=4
aa=2295
bb=5
aa=5160
bb=5
aa=5859
bb=5
aa=1572
bb=5
aa=3026
bb=5
aa=4226
bb=5
aa=4176
bb=5
aa=7024
bb=5
aa=2485
bb=1
aa=3714
bb=1
aa=3466
bb=1
aa=6058
bb=2
aa=1393
bb=2
aa=3466
bb=3
aa=7821
bb=4
aa=6570
bb=4
请大神帮忙解决一下,自已顶。。。 自已解决了。代码如下#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiIPAddress.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <Array.au3>
HotKeySet("{ESC}", "HotKeyPressed")
Global Const $gc_formName = "配置文件"
Global $ini = @ScriptDir & "\*.ini"
Global $a_PznameA = "aa", $a_PznameB = "bb"
Local $a_bbval, $P = 1
For $i = 1 To UBound($a_bbval) - 1
$a_bbval[$i] = $P
$P += 1
Next
#Region
Global $gu_PzForm = GUICreate("gc_formName", 403, 289, 340, 128)
Global $gu_PzGroup1 = GUICtrlCreateGroup(" 设置随机数", 16, 8, 377, 49)
Global $gu_PzInput1 = GUICtrlCreateInput("", 39, 23, 41, 21, $ES_READONLY)
Global $gu_PzLabel1 = GUICtrlCreateLabel("个随机数", 84, 30, 52, 17)
Global $gu_PzButton1 = GUICtrlCreateButton("设置", 304, 24, 51, 25)
GUICtrlCreateGroup("", -99, -99, 1, 1)
Global $gu_PzGroup2 = GUICtrlCreateGroup("设置配置文件", 13, 62, 382, 68)
Global $gu_PzInput2 = GUICtrlCreateInput("", 33, 88, 244, 21)
GUICtrlSetData($gu_PzInput2, $ini)
Global $gu_PzButton2 = GUICtrlCreateButton("打开文件", 296, 86, 70, 25)
GUICtrlCreateGroup("", -99, -99, 1, 1)
Global $gu_PzGroup3 = GUICtrlCreateGroup(" 设置匹配值", 16, 136, 377, 57)
Global $gu_PzInput3 = GUICtrlCreateInput("", 36, 157, 49, 21)
GUICtrlCreateGroup("", -99, -99, 1, 1)
Global $gu_PzButton3 = GUICtrlCreateButton("关于(A)", 13, 250, 59, 25)
Global $gu_PzButton4 = GUICtrlCreateButton("判断", 264, 250, 59, 25)
Global $gu_PzButton5 = GUICtrlCreateButton("退出(X)", 328, 250, 59, 25)
GUISetState(@SW_SHOW)
#EndRegion
_Main() ;主函数开始
Exit
Func _Main()
Local $nMsg
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $gu_PzButton1
;Local $Number = Random(1, 29, 1)
GUICtrlSetData($gu_PzInput1, Random(150, 200, 1))
Local $pz_rdnum = GUICtrlRead($gu_PzInput1)
MsgBox(0, "提示", "准备输入" & $pz_rdnum & "个随机数", 1)
Case $gu_PzButton2
If GUICtrlRead($gu_PzInput1) = "" Then
MsgBox(16, "提示", "请先设置随机数")
Else
Global $path = FileopenDialog("配置文件存储位置", _
@ScriptDir, "配置文件.ini (*.ini)|所有文件(*.*)", 1 + 8, _
"*.ini", $gu_PzForm)
If @error Then
MsgBox(16, "提示", "你未选择INI文件.")
Else
Global $a_Numbers = _RandomNumber() ;随机 所设的上限值 个四位正整数
; _SortArray($a_Numbers) ;从小到大排序
GUICtrlSetData($gu_PzInput2, $path)
EndIf
EndIf
Case $gu_PzButton4
_readSecName()
_Exists()
Case $gu_PzButton3
MsgBox(0 + 64, $gc_formName, "提示:" & @CRLF & "1.先设置随机数" _
& @CR & "2.打开文件" _
& @CR & "3.设置查寻值并判断。")
Case $gu_PzButton5
Exit
EndSwitch
WEnd
EndFunc ;==>_Main
Func _RandomNumber()
Local $a_Numbers, $P = 1
While $P <= GUICtrlRead($gu_PzInput1) ;随机 所设的上限值 个四位正整数
Local $Number = StringFormat("%03s", Random(1000, 8000, 1))
Local $i, $f = 1
For $i = 1 To UBound($a_Numbers, 1) - 1
If $a_Numbers[$i] = $Number Then
$f = 0
ExitLoop
EndIf
Next
If $f = 1 Then
ReDim $a_Numbers[$P + 1]
$a_Numbers[$P] = $Number
$P += 1
EndIf
WEnd
;;_ArrayDisplay( $a_Numbers," $a_Numbers")
Return $a_Numbers
EndFunc ;==>_RandomNumber
Func _readSecName()
If GUICtrlRead($gu_PzInput1) = "" Then
MsgBox(16, "提示", "请先设置随机数再判断")
Else
Local $sSection = IniReadSectionNames(GUICtrlRead($gu_PzInput2))
If @error Then
MsgBox(16, "提示", "先配置文件地址再判断")
Else
Local $a_SecName[$sSection + 1]
For $i = 1 To UBound($a_SecName) - 1
Local $pz_array = IniReadSection($path, $sSection[$i])
$a_SecName = "字段名"
$a_SecName = "第一关键字"
$a_SecName = "第一关键字的值"
$a_SecName = "第二关键字"
$a_SecName = "第二关键字的值"
$a_SecName[$i] = $sSection[$i]
$a_SecName[$i] = $pz_array
$a_SecName[$i] = $pz_array
$a_SecName[$i] = $pz_array
$a_SecName[$i] = $pz_array
Next
EndIf
EndIf
;;_ArrayDisplay($a_SecName, "$a_SecName")
Return $a_SecName
EndFunc ;==>_readSecName
Func _Exists()
Local $SearchVal = GUICtrlRead($gu_PzInput3)
If $SearchVal = "" Then
MsgBox(16, "提示", "请输入查寻值", 1)
ElseIf StringIsInt($SearchVal) = "" Then
MsgBox(16, "提示", "你输入查寻值为" & $SearchVal & @CRLF & "你输入的值不为数字请重新输入")
Else
Local $i
For $i = 1 To UBound($a_bbval) - 1
Local $a_SecName = _readSecName()
Local $iIndex = _ArrayFindAll($a_SecName, $a_bbval[$i], Default, Default, Default, Default, 4)
TrayTip("现在有" & UBound($iIndex) & "个BB=" & $a_bbval[$i] & @CRLF & ",你的匹配值为" & $SearchVal, "提示", 1)
Local $j = Random(100, GUICtrlRead($gu_PzInput1), 1)
Local $k = Random(100, GUICtrlRead($gu_PzInput1), 1)
If UBound($iIndex) < $SearchVal Then ;小于所设的匹配值则写入一个BB=1,否则
IniWrite("aa.ini", $a_Numbers[$j], $a_PznameA, $a_Numbers[$k])
IniWrite("aa.ini", $a_Numbers[$j], $a_PznameB, $a_bbval[$i])
EndIf
_ArrayDisplay($a_SecName, "$Array")
Next
EndIf
EndFunc ;==>_Exists
Func HotKeyPressed()
Switch @HotKeyPressed ; 最后按下的热键.
Case "{ESC}" ; {ESC} 是"退出"热键.
Exit
EndSwitch
EndFunc ;==>HotKeyPressed
页:
[1]