首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在AppleScript中通过虚线递增?

在AppleScript中通过虚线递增?
EN

Stack Overflow用户
提问于 2017-09-26 16:04:12
回答 1查看 91关注 0票数 1

我试图检查值,以获得准确的计数,但我在步进过程中遇到了问题。考虑到以下情况:

代码语言:javascript
复制
1-1
2-1
3-1
4-1
5-1
6-1
7-1
7-2
8-1
9-1
9-2
9-3
9-4
10-1
11-1
12-2 ## intentionally left out 12-1 to throw error
13-1

如果缺少一个,如何通过列表和标志正确地递增。当我运行我的脚本时,它会通过7-2运行,但是当涉及到8-1时,它会失败,因为:

一个儿童标记似乎不见了。

守则:

代码语言:javascript
复制
tell application "BBEdit"
    activate
    set parentCount to 1
    set childCount to 1
    set theDoc to text document 1
    select insertion point before first character of theDoc
    set searchOpt to {search mode:grep, wrap around:false}
    repeat
        set theNumbers to find "^(\\d{1,4})-(\\d{1,4})" searching in text 1 of theDoc options searchOpt with selecting match
        if not found of theNumbers then exit repeat
        set parentNum to (grep substitution of "\\1") as number
        set childNum to (grep substitution of "\\2") as number
        if parentNum is equal to parentCount and childNum is equal to childCount then
            set parentCount to parentCount + 1
        else if parentNum is equal to parentCount and (childNum + 1) is equal to childCount then
            set parentCount to parentCount + 1
            set childCount to 1
        else
            display dialog "missing marker"
        end if
    end repeat
    display dialog "completed check"
end tell

在AppleScript中,如何通过数字序列正确地递增?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-26 17:38:38

在条件语句中,使用相同的条件if parentNum is equal to parentCount,第二个条件语句不能工作,因为脚本在这两个条件语句中增加了parentCount,并且脚本从不增加childCount。

使用以下脚本:

代码语言:javascript
复制
tell application "BBEdit"
    activate
    set parentCount to 0
    set childCount to 1
    set theDoc to text document 1
    select insertion point before first character of theDoc
    set searchOpt to {search mode:grep, wrap around:false}
    repeat
        set theNumbers to find "^(\\d{1,4})-(\\d{1,4})" searching in text 1 of theDoc options searchOpt with selecting match
        if not found of theNumbers then exit repeat
        set parentNum to (grep substitution of "\\1") as number
        set childNum to (grep substitution of "\\2") as number
        if parentNum = (parentCount + 1) and childNum = 1 then --  if the parentNum increase of 1, the childNum must be 1
            set parentCount to parentCount + 1
            set childCount to 1 -- so, reset the childCount  to 1
        else if parentNum = parentCount and childNum = (childCount + 1) then
            set childCount to childNum
        else
            display dialog "missing marker"
            set parentCount to parentNum -- start at this value for the next sequence
            set childCount to childNum -- start at this value for the next sequence
        end if
    end repeat
    display dialog "completed check"
end tell
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46431151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档