AutoHotkey Community

It is currently May 27th, 2012, 1:32 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: April 11th, 2009, 8:35 pm 
Offline

Joined: November 4th, 2008, 9:23 am
Posts: 1045
Functions: in

Description


Download
in.zip

Requirements
None



Functions
in(Value, MatchList, Delimiters = ",", OmitChars = "")
Modified wrapper for if var in MatchList that makes use of a parsing loop.

ReturnValue
If a match is found, the index of the substring that matches the specified value.

If no match is found, the function returns 0.

Remarks
The values are compared using a case-insensitive equal (=).

Numbers are compared numerically.

If a match is found, ErrorLevel is set to the matching substring. If no match is found, ErrorLevel is not changed.



Code
Code:
in(Value, MatchList, Delimiters = ",", OmitChars = "")
{
    Loop, Parse, MatchList, %Delimiters%, %OmitChars%
    {
        if (A_LoopField = Value)
        {
            ;store the match (as reflected in the Match list)
            ErrorLevel := A_LoopField

            return A_Index
        }
    }

    return false
}


Example 1
Code:
var := 5.0

;returns true - in the list
if in(var, "1,2,3,5,7,11") ;Avoid spaces in list.
    MsgBox inA: %var% is a small prime number.

;returns false - not in the list
;Note that it compares the values as strings, not numbers.
if var in 1,2,3,5,7,11
    MsgBox %var% is a small prime number.


if in(var, "1,2, 3, 5 , 7 , 11"  ,  ","  ,  " ") ;Don't worry about spaces
    MsgBox inB: %var% is a small prime number.


Example 2
Code:
List =
(LTrim
    iTem1
    ItEm2
    iTem3
)

value := "item2"

;works great with continuation sections
if index := in(value, List, "`n")
{
    ;ErrorLevel contains the matching substring ("iTem1")
    MsgBox, "%value%" matches list item %index%.`nThe matching substring is: %ErrorLevel%
}




How to use
Extract the zip's contents to a library folder for automatic inclusion - StdLib compliant.

A copy of the above examples can be found in the "Func Examples" folder.


Download in function

_________________
As always, if you have any further questions, don't hesitate to ask.

Add OOP to your scripts via the Class Library. Check out my scripts.


Last edited by animeaime on April 12th, 2009, 5:41 pm, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 11th, 2009, 10:03 pm 
Thx :D


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bon and 15 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group