Jump to content

How would you rewrite this method with less duplicate code?


Recommended Posts

This code works perfectly fine for my purposes.

However, it's a little ugly because there is obvious duplication.

I've been playing with this and regular expressions for about an hour and could not seem to merge the check into a single expression.

But I'm quite certain it must be possible to have optional matches...

Any tips, even if it is simply a suggestion of what keywords I should be Googling?

Func GetNthValueFromStatubar($n)
    ; Get status text as string.
    $Status = StatusbarGetText($MainWindowTitle)
    ; Find value based on N.
    If $n = 1 Then
        $Result = StringRegExp( $Status, "[A-Za-z: ]+\$([A-Z0-9]+)", 1 )
    ElseIf $n = 2 Then
        $Result = StringRegExp( $Status, "[A-Za-z: ]+\$([A-Z0-9]+)[\s]*[A-Za-z: ]+\$([A-Z0-9]+)", 1 )
    ElseIf $n = 3 Then
        $Result = StringRegExp( $Status, "[A-Za-z: ]+\$([A-Z0-9]+)[\s]*[A-Za-z: ]+\$([A-Z0-9]+)[\s]*[A-Za-z: ]+\$([A-Z0-9]+)", 1 )
    EndIf
    Return $Result[$n - 1]
EndFunc
Link to comment
Share on other sites

The following achieved the what I was going for:

Func GetNthValueFromStatubar($n)
    ; Get status text as string.
    $Status = StatusbarGetText($MainWindowTitle)
    ; Find values within the string.
    $Result = StringRegExp( $Status, "[A-Za-z: ]+\$([A-Z0-9]+)(?:[\s]*[A-Za-z: ]+\$([A-Z0-9]+))?(?:[\s]*[A-Za-z: ]+\$([A-Z0-9]+))?", 1 )
    ; Return the desired value.
    Return $Result[$n - 1]
EndFunc
Edited by joncom
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...