Home > Net >  iterate through string to get match object from the List in AppleScript
iterate through string to get match object from the List in AppleScript

Time:07-30

I have tried this below code, It should display "Match". But Somehow it isn't.

set mylist to {"wade", "lee"}
set username to "sarawade"
set a to (count username)

repeat a times
    set mystring to do shell script "echo " & username & " | sed 's/[a-z]//'"
    set username to mystring
    display dialog username
    repeat with theitem in mylist
        if username = theitem then
            display dialog "Match"
        end if
    end repeat
end repeat

CodePudding user response:

You compare username with theitem, which is a reference to list's element. You should compare to contents of theitem instead:

set mylist to {"wade", "lee"}
set username to "sarawade"
set a to (count username)

repeat a times
    set mystring to do shell script "echo " & username & " | sed 's/[a-z]//'"
    set username to mystring
    display dialog username
    repeat with theitem in mylist
        if username = contents of theitem then -- EDITED
            display dialog "Match"
        end if
    end repeat
end repeat

NOTE: you can do it easy:

set mylist to {"wade", "lee"}
set username to "sarawade"

display dialog username with title "The username"

set matchFound to false
repeat with theitem in mylist
    if username ends with (contents of theitem) then
        set matchFound to true
        exit repeat
    end if
end repeat

if matchFound then
    return contents of theitem
else
    return "No match found"
end if
  • Related