touch_xu 发表于 2011-3-27 22:25:11

【已解决】Return 函数值返回问题求简化写法

本帖最后由 touch_xu 于 2011-3-28 21:54 编辑

以下两个函数功能正常,但是这简单的功能,却要写得这么复杂,求简化写法 谢谢

$_IDRead = FileReadLine("C:\Flextest.cfg\Station_ID.txt", 1)
Func _Stationbg()
        If StringInStr($_IDRead, "OQA") Then
                Return "OQA"
        ElseIf StringInStr($_IDRead, "T&R") Then
                Return "T&R"
        ElseIf StringInStr($_IDRead, "FINAL") Then
                Return "FINAL"
        ElseIf StringInStr($_IDRead, "Color") Then
                Return "Color"
        ElseIf StringInStr($_IDRead, "FOCUS") Then
                Return "FOCUS"
        ElseIf StringInStr($_IDRead, "Power") Then
                Return "Current"
        Else
                Return ""
        EndIf
EndFunc   ;==>_Stationbg

Func _StationDut()
        If StringInStr($_IDRead, "OQA") And StringInStr($_IDRead, "COLOR") Then
                Return "OQACOLOR"
        ElseIf StringInStr($_IDRead, "OQA") And StringInStr($_IDRead, "T&R") Then
                Return "OQAT&R"
        ElseIf StringInStr($_IDRead, "OQA") And StringInStr($_IDRead, "FINAL") Then
                Return "OQAFINAL"
        ElseIf StringInStr($_IDRead, "COLOR") Then
                Return "COLOR"
        ElseIf StringInStr($_IDRead, "OQA") Then
                Return "OQA"
        ElseIf StringInStr($_IDRead, "T&R") Then
                Return "T&R"
        ElseIf StringInStr($_IDRead, "FINAL") Then
                Return "FINAL"
        ElseIf StringInStr($_IDRead, "FOCUS") Then
                Return "FOCUS"
        ElseIf StringInStr($_IDRead, "Power") Then
                Return "Power"
        Else
                Return ""
        EndIf
EndFunc   ;==>_StationDut

afan 发表于 2011-3-27 22:39:11

本帖最后由 afan 于 2011-3-27 22:46 编辑

这样写比较清晰吧,没什么不好~ 或者可以这样Func _Stationbg()
        Local $sR = StringRegExp($_IDRead, '(?i)OQA|T&R|FINAL|Color|FOCUS|Power', 1)
        If @error Then Return ''
        If $sR <> 'Power' Then Return $sR
        Return 'Current'
EndFunc   ;==>_Stationbg

easefull 发表于 2011-3-28 00:15:23

觉得elseif太多的话,还是用select...case比较好一些.

annybaby 发表于 2011-3-28 00:30:38

回复 2# afan


    再一次见识到了正则表达式的威力,膜拜中~~

touch_xu 发表于 2011-3-28 09:27:54

Afan正则所到处 必有精彩

touch_xu 发表于 2011-3-28 09:27:56

Afan正则所到处 必有精彩

touch_xu 发表于 2011-3-28 14:13:49

请问如何不区别大小写,并且可以返回多个值呢

netegg 发表于 2011-3-28 14:56:54

本帖最后由 netegg 于 2011-3-28 16:51 编辑


Func _Stationbg($txt)
    If StringInStr($_IDRead, $txt) Then $ret = $txt
    If StringInStr($_IDRead, 'Power') Then $ret = "Current"
    Return $ret
EndFunc   ;==>_Stationbg


Func _StationDut($txt, $ext)
    If StringInStr($_IDRead, $txt) Then $ret = $txt
    If $txt = "OQA" And StringInStr($_IDRead, $ext) Then $ret &= $ext
    Return $ret
EndFunc   ;==>_StationDut
又看了看楼主的问题,好像理解错了,凑合看吧

easefull 发表于 2011-3-28 16:13:09

Global $_IDRead = "Power|OQA|T&R"
MsgBox(4096, "_Stationbg" , _Stationbg() )
MsgBox(4096, "_StationDut", _StationDut())

Func _StationDut()
        Local $sResult
        If Not StringRegExp($_IDRead, 'OQA', 0) Then
                $sResult = _Stationbg('(?i)Color|OQA|T&R|FINAL|FOCUS|Power')
                If $sResult == "Current" Then $sResult = "Power"
                Return $sResult
        Else
                $sResult= "OQA"
                $sResult &= _Stationbg('(?i)Color|T&R|FINAL')
                Return $sResult
        EndIf
EndFunc   ;==>_StationDut

Func _Stationbg($sRegExp = '(?i)OQA|T&R|FINAL|Color|FOCUS|Power')
        Local $asResult = StringRegExp($_IDRead, $sRegExp, 1)
        If @error Then
                Return ''
        Else
                If $asResult == "Power" Then $asResult = "Current"
                Return $asResult
        EndIf
EndFunc   ;==>_Stationbg

touch_xu 发表于 2011-3-28 21:53:28

感谢大家 感谢easefull已解决

xdcysten 发表于 2013-3-16 04:13:53

A版的正则很厉害,学习了
页: [1]
查看完整版本: 【已解决】Return 函数值返回问题求简化写法