2D List(2次元配列)を右に90度回転させるAppleScriptです。
回転させるにあたって、各行のアイテム数のうち最大のものを求め、最大アイテム数に足りない行については詰め物をして、2次元配列をなるべく正方形ないし長方形に整った形にしてから回転させます。
AppleScript名:2Dリストを右(clockwise)に90度回転 |
— Created 2017-10-02 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" property NSArray : a reference to current application’s NSArray set origList to {{1, 2, 3, 4, 5, 6, 7}, {1, 2, 3, 4, 5, 6}, {1, 2, 3, 4, 5, 6, 7, 8}} set eList to rotateListClockwise90(origList, 0) of me –> {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {4, 4, 4}, {5, 5, 5}, {6, 6, 6}, {7, 0, 7}, {8, 0, 0}} (*) set sList to rotateListClockwise90(eList, 0) of me –> {{8, 7, 6, 5, 4, 3, 2, 1}, {0, 0, 6, 5, 4, 3, 2, 1}, {0, 7, 6, 5, 4, 3, 2, 1}} set wList to rotateListClockwise90(sList, 0) of me set nList to rotateListClockwise90(wList, 0) of me set curMax to 0 set curLen to length of aList set curMax to getMaxItemCountFrom2DArray(aList) of me set tmpList to {} set twoDList to make2DBlankArray(curLen, curMax) of me set curY to 1 repeat with x from 1 to curMax set curX to 1 repeat with y from curLen to 1 by -1 set aCon to getItemByXY(x, y, aList, blankItem) of me set twoDList to setItemByXY(curX, curY, twoDList, aCon) of me set curX to curX + 1 end repeat set curY to curY + 1 end repeat return twoDList end rotateListClockwise90 on getMaxItemCountFrom2DArray(curList) set anArray to NSArray’s arrayWithArray:curList set eRes to (anArray’s valueForKeyPath:"@unionOfObjects.@count")’s valueForKeyPath:"@max.self" return eRes as integer end getMaxItemCountFrom2DArray –2D Listに配列の添字的なアクセスを行なってデータを取得 on getItemByXY(aX, aY, aList, aBlankItem) –1 based index try set aContents to contents of (item aX of item aY of aList) on error set aContents to aBlankItem end try return aContents end getItemByXY –2D Listに配列の添字的なアクセスを行なってデータを設定 on setItemByXY(aX, aY, tmpList, aContents) –1 based index set (item aX of item aY of tmpList) to aContents return tmpList end setItemByXY –空白の2D Array を出力する on make2DBlankArray(curLen, curMax) set outArray to {} repeat curMax times set tmpList to {} repeat curLen times set the end of tmpList to "" end repeat set the end of outArray to tmpList end repeat return outArray end make2DBlankArray |
More from my site
(Visited 106 times, 2 visits today)
2D Listの行列入れ替え(transpose) – AppleScriptの穴 says:
[…] ら、単純にデータを(画像のように)回転させるだけの処理のはずですが(→ 時計まわりに90度回すサンプル)、そういうわけでもない。なんでこんな処理をShaneが書いたのか理解でき […]