且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何读取/修改/删除applescript中列表中包含的项目/列表

更新时间:2023-11-28 16:46:28

如果你自己编写一个子程序来为你做这些事情,那真的并不难.这是您要求的 3 个.您需要添加其他子例程,以便您可以执行其他数学运算(加、减和除).只需复制multiplyItemNumberByValue() 子例程并将运算符更改为适当的操作符,它应该无需进一步更改即可工作.

It's really not hard if you write yourself a subroutine to do the stuff for you. Here's the 3 you requested. You'll need to add other subroutines so you can do other math operations (add, subtract, and divide). Just duplicate the multiplyItemNumberByValue() subroutine and change the operator to the appropriate one and it should work with no further changes.

祝你好运.

set listOfLists to {{"1", "2+2", "3", "4", "5", "6"}, {"6", "5", "4", "3", "2", "1"}, {"7", "8", "9", "0", "7", "6"}, {"3", "4", "4", "5+3", "3", "1"}}

-- before performing any other operation, expand all zeros
set listOfLists to expandZeros(listOfLists)

-- remove item 2 from every sublist
set listOfLists to removeItemNumber(listOfLists, 2)

-- multiply item 1 of every sublist by 5
set listOfLists to multiplyItemNumberByValue(listOfLists, 1, 5)




(****************** SUBROUTINES ******************)
on multiplyItemNumberByValue(listOfLists, itemNumber, theValue)
    set newList to {}
    repeat with i from 1 to count of listOfLists
        set thisList to item i of listOfLists
        set thisItem to item itemNumber of thisList
        set newValue to (thisItem as number) * theValue
        set item itemNumber of thisList to (newValue as text)
        set end of newList to thisList
    end repeat
    return newList
end multiplyItemNumberByValue

on removeItemNumber(listOfLists, itemNumber)
    set newList to {}
    repeat with i from 1 to count of listOfLists
        set thisList to item i of listOfLists
        if itemNumber is equal to 1 then
            set newSublist to items 2 thru end of thisList
        else if itemNumber is equal to (count of thisList) then
            set newSublist to items 1 thru (itemNumber - 1) of thisList
        else
            set newSublist to items 1 thru (itemNumber - 1) of thisList & items (itemNumber + 1) thru end of thisList
        end if
        set end of newList to newSublist
    end repeat
    return newList
end removeItemNumber

on expandZeros(listOfLists)
    set newList to {}
    repeat with i from 1 to count of listOfLists
        set thisList to item i of listOfLists
        set newSublist to {}
        repeat with j from 1 to count of thisList
            set subItem to item j of thisList
            if subItem contains "+" then
                set x to offset of "+" in subItem
                if x is equal to 0 or x is equal to 1 or x is equal to (count of subItem) then
                    set end of newSublist to subItem -- do nothing
                else
                    set a to text 1 thru (x - 1) of subItem
                    set b to text (x + 1) thru end of subItem
                    repeat (b as number) times
                        set a to a & "0"
                    end repeat
                    set end of newSublist to a
                end if
            else
                set end of newSublist to subItem -- do nothing
            end if
        end repeat
        set end of newList to newSublist
    end repeat
    return newList
end expandZeros

这是其他数学子程序...

Here's the other math subroutines...

on divideItemNumberByValue(listOfLists, itemNumber, theValue)
    set newList to {}
    repeat with i from 1 to count of listOfLists
        set thisList to item i of listOfLists
        set thisItem to item itemNumber of thisList
        set newValue to (thisItem as number) / theValue
        set item itemNumber of thisList to (newValue as text)
        set end of newList to thisList
    end repeat
    return newList
end divideItemNumberByValue

on addValueToItemNumber(listOfLists, itemNumber, theValue)
    set newList to {}
    repeat with i from 1 to count of listOfLists
        set thisList to item i of listOfLists
        set thisItem to item itemNumber of thisList
        set newValue to (thisItem as number) + theValue
        set item itemNumber of thisList to (newValue as text)
        set end of newList to thisList
    end repeat
    return newList
end addValueToItemNumber

on subtractValueFromItemNumber(listOfLists, itemNumber, theValue)
    set newList to {}
    repeat with i from 1 to count of listOfLists
        set thisList to item i of listOfLists
        set thisItem to item itemNumber of thisList
        set newValue to (thisItem as number) - theValue
        set item itemNumber of thisList to (newValue as text)
        set end of newList to thisList
    end repeat
    return newList
end subtractValueFromItemNumber