If it equals?

Ken Ray kray at sonsothunder.com
Thu Aug 22 13:29:00 EDT 2002


> on mouseup ## or whatever
>    set the itemdel to TAB
>    ask "Who the hell are YOU ???"
>    if it is empty then exit mouseup
>    put it into the_user
>    repeat for each line l in myFile
>       if item 1 of l is the_user then
>         put item 2 of l into the_password
>         exit repeat
>     else
>        ## heavy complaining...
>        exit mouseup
>      end if
>   ask "Please enter more money ehmm your password"
>   if it is the_password then
>      ### do_the_right_thing
>   else
>   ## heavy complaining...
>   end if
> end mouseup


Or, using regular expressions:

function getPassword userName,userFilePath
    local tPassword
    put url ("file:" & userFilePath) into userData
    repeat for each line l in userData
        get matchText(l,"^" & userName & tab & "(.*$)", tPassword)  -- *
        if it is true then return tPassword
    end repeat
    return "Password not found for user " & userName
end getPassword

* = Read: "In line l, find userName and tab at the beginning of the line
[^], followed by one or more characters [.*] at the end of the line [$], and
return the value you find from the tab to the end of the line [the
parentheses in "(.*$)"].

And when Rev 1.5 comes out with full PCRE support, you don't need a repeat
loop:

function getPassword userName,userFilePath
    local tPassword
    put url ("file:" & userFilePath) into userData
    get matchText(cr & userData & cr,"\n" & userName & tab &
"(.*?)\n",tPassword)  --**
    if it is true then return tPassword
    else return "Password not found for user " & userName
end getPassword

** Since we're going to use CRs for the beginning and ending boundaries in a
full text search, the text to check is bounded by CRs (cr & userData & cr),
and the regular expression is similarly bounded by crs (\n on both sides).
Read it as this: "In userData, find the userName and tab preceded by a
return [\n], followed by one or more characters in a non-greedy way (that
is, stop when you find the first match - don't go on to find any more
matches) [.*?], followed by another return [\n], and then return the value
you find from the tab to the return character [the parentheses in "(.*?)"].

Am I the only one that sees these regular expressions as cool?  :-)

Ken Ray
Sons of Thunder Software
Email: kray at sonsothunder.com
Web Site: http://www.sonsothunder.com/





More information about the Use-livecode mailing list