从列表中随机选择一个项目,然后删除包含与随机选择的项目相同单词的任何后续列表项目?

问题描述 投票:0回答:2

我正在编写一个脚本,通过从列表中随机选择菜肴 7 次来创建食物菜单。然而,我想让它不选择任何类似的菜肴。例如,如果“羊排”被选为

dish1
,那么
dish2
将是同一列表中的随机项目,其中包括“羊肉”和“排骨”在内的所有选项均被删除。然后,如果
dish2
是“意大利肉酱面”,那么
dish3
将从同一列表中选择,不包括“羊肉”、“排骨”、“意大利面”和“肉酱面”,依此类推。有人可以帮忙吗?

-谢谢。

list applescript
2个回答
3
投票

我对 AppleScript 有点生疏,所以如果我犯了错误,请原谅。

分解你必须做什么,

1) 随机选择一个项目

-- The initial menuItems we start off with
set menuItems to {"Lamb Chops", "Lamb", "Chops", "Spaghetti Bolognese", "Spaghetti", "Bolognese"}

set randomItemNum to random number from 1 to count of menuItems
set randomItem to item randomItemNum of menuItems as string

2) 重复

menuItems
,这样我们就可以从
randomItem

中排除包含单词的项目,我们将使用此子例程来拆分字符串:

on splitString(theString, theDelimiter)
    -- save delimiters to restore old settings
    set oldDelimiters to AppleScript's text item delimiters
    -- set delimiters to delimiter to be used
    set AppleScript's text item delimiters to theDelimiter
    -- create the array
    set theArray to every text item of theString
    -- restore the old setting
    set AppleScript's text item delimiters to oldDelimiters
    -- return the result
    return theArray
end splitString

现在我们可以轻松地分割字符串,让我们继续重建菜单

-- This loops through menuItems, and we can use menuItem to access the current looped item
-- We'll use this to reconstruct menuItems, but excluding what we need

set newMenuItems to {}
repeat with menuItem in menuItems
    -- Get words from randomItem
    -- For example, if randomItem is "Lamb Chops", it will return {"Lamb","Chops"}
    set menuItemWords to splitString(randomItem, " ")

    -- This is the conditional that will determine to include menuItem or not
    -- Feel free to change this to get the outcome you want
    if menuItemWords does not contain menuItem as string and menuItem does not contain menuItemWords then

        -- And this conditional to not re-add the randomItem
        -- However, it does it to one word items in the previous if statement
        -- Note: Without using "as string" to menuItem it doesn't work correctly, not sure why          
       if menuItem as string is not equal to randomItem then
            set the end of newMenuItems to menuItem
        end

    end

end repeat
-- Now we've finished creating a new menu with the changes we want, set it to the main one
set menuItems to newMenuItems

而且,现在应该可以了。

您可能还想轻松检查结果,在这种情况下,您可以使用此子例程将数组连接成字符串:

on arrayToString(theArray, theSeperator)
    set arrayString to ""
    repeat with theItem in theArray
        if arrayString is equal to "" then
            set arrayString to theItem
        else
            set arrayString to arrayString & theSeperator & theItem
        end if
    end repeat
end arrayToString

并在将菜单设置为新菜单之前显示结果。

........

display alert "I've picked " & randomItem & " for you." message "Old menu:
" & arrayToString(menuItems, ", ") & "
New menu:
" & arrayToString(newMenuItems, ", ")

-- Now we've finished creating a new menu with the changes we want and displayed the results, set it to the main one
set menuItems to newMenuItems


您还可以在指定初始 menuItem 后将代码扭曲在
repeat 7 times
中,并在 menuItems 为空时检查退出重复,从而循环浏览菜单项 7 次,如下所示:

on splitString(theString, theDelimiter)
    ...
end splitString

-- The initial menuItems we start off with
set menuItems to {"Lamb Chops", "Lamb", "Chops", "Spaghetti Bolognese", "Spaghetti", "Bolognese"}

-- Repeat statement to loop 7 times
repeat 7 times
  -- If menuItems is empty, exit the repeat
  if count of menuItems is 0 then
      exit repeat
  end if

  set randomItemNum to random number from 1 to count of menuItems
  set randomItem to item randomItemNum of menuItems as string

  set newMenuItems to {}
  repeat with menuItem in menuItems
      ...
  end repeat
  -- Now we've finished creating a new menu with the changes we want, set it to the main one
  set menuItems to newMenuItems

-- This is the end of loop for 7 times
end repeat


我希望这有帮助。
如果您需要任何其他帮助,或者有错误,请在下面发表评论,我会尽力而为。
编辑
这是应该满足您需要的完整代码,它重复 7 次,并在完成后向用户显示所选菜肴的列表:

on splitString(theString, theDelimiter)
    -- save delimiters to restore old settings
    set oldDelimiters to AppleScript's text item delimiters
    -- set delimiters to delimiter to be used
    set AppleScript's text item delimiters to theDelimiter
    -- create the array
    set theArray to every text item of theString
    -- restore the old setting
    set AppleScript's text item delimiters to oldDelimiters
    -- return the result
    return theArray
end splitString

on arrayToString(theArray, theSeperator)
    set arrayString to ""
    repeat with theItem in theArray
        if arrayString is equal to "" then
            set arrayString to theItem
        else
            set arrayString to arrayString & theSeperator & theItem
        end if
    end repeat
end arrayToString

-- The menu after you finish looping to show the user
set yourMenu to {}

-- The initial menuItems we start off with
set menuItems to {"Lamb Chops", "Lamb", "Chops", "Spaghetti Bolognese", "Spaghetti", "Bolognese"}

-- Repeat statement to loop 7 times
repeat 7 times
    -- If menuItems is empty, exit the repeat
    if (count of menuItems) is 0 then
        exit repeat
    end if

    set randomItemNum to random number from 1 to count of menuItems
    set randomItem to item randomItemNum of menuItems as string

    -- This loops through menuItems, and we can use menuItem to access the current looped item
    -- We'll use this to reconstruct menuItems, but excluding what we need
    set newMenuItems to {}
    repeat with menuItem in menuItems
        -- Get words from randomItem
        -- For example, if randomItem is "Lamb Chops", it will return {"Lamb","Chops"}
        set menuItemWords to splitString(randomItem, " ")

        -- This is the conditional that will determine to include menuItem or not
        -- Feel free to change this to get the outcome you want
        if menuItemWords does not contain menuItem as string and menuItem does not contain menuItemWords then

            -- And this conditional to not re-add the randomItem
            -- However, it does it to one word items in the previous if statement
            -- Note: Without using "as string" to menuItem it doesn't work correctly, not sure why          
            if menuItem as string is not equal to randomItem then
                set the end of newMenuItems to menuItem
            end if

        end if

    end repeat

    -- Now we've finished creating a new menu with the changes we want, set it to the main one
    set menuItems to newMenuItems
    set the end of yourMenu to randomItem

    -- This is the end of loop for 7 times
end repeat

set itemSelected to (choose from list yourMenu with prompt "Here's your menu.")
display alert "You selected " & itemSelected & "."

0
投票

紧凑型:

set menuItems to {"Lamb Chops", "Lamb", "Chops", "Spaghetti Bolognese", "Spaghetti", "Bolognese"}
set randomItem to item (random number from 1 to count of menuItems) of menuItems
© www.soinside.com 2019 - 2024. All rights reserved.