スクリプト/配列を比較する関数/array_comparison関数

最終投稿者:Material 185033 3 mini 光楼(114) 更新:2019/07/21 12:31:59
2つの配列を比較し、同じものか判定する関数です。

Rmakeの仕様上、文字列の数字と、普通の数字を同じものと扱うので注意が必要です。
また、0とfalse、1とtrueも同様です。

完全な比較は出来ませんのでご注意ください。

  • 関数
def array_comparison(arr1, arr2)
  decision = true
  arr_l = getArrayLength(arr1)
  if arr_l == getArrayLength(arr2)
    i = 0
    while i < arr_l
      if arr1[i] != arr2[i]
        decision = false
        i = arr_l
      end
      i = i + 1
    end
  else
    decision = false
  end

  return decision
end


  • 実行結果
arr1 = createArray()
arr1[0] = 0
arr1[1] = 1
arr1[2] = 2

arr2 = createArray()
arr2[0] = 0
arr2[1] = 1
arr2[2] = 2

speak(array_comparison(arr1, arr2))
#true

arr1 = createArray()
arr1[0] = 0
arr1[1] = 1
arr1[2] = 2

arr2 = createArray()
arr2[0] = 0
arr2[1] = "1"
arr2[2] = 2

speak(array_comparison(arr1, arr2))
#true

arr1 = createArray()
arr1[0] = 0
arr1[1] = 1
arr1[2] = 2

arr2 = createArray()
arr2[0] = false
arr2[1] = true
arr2[2] = 2

speak(array_comparison(arr1, arr2))
#true

arr1 = createArray()
arr1[0] = "0"
arr1[1] = "1"
arr1[2] = 2

arr2 = createArray()
arr2[0] = false
arr2[1] = true
arr2[2] = 2

speak(array_comparison(arr1, arr2))
#true

arr1 = createArray()
arr1[0] = "0"
arr1[1] = "1"
arr1[2] = 2

arr2 = createArray()
arr2[0] = "false"
arr2[1] = "true"
arr2[2] = 2

speak(array_comparison(arr1, arr2))
#false

arr1 = createArray()
arr1[0] = 0
arr1[1] = 1
arr1[2] = 2

arr2 = createArray()
arr2[0] = 0
arr2[2] = 2

speak(array_comparison(arr1, arr2))
#false

arr1 = createArray()
arr1[0] = "あ"
arr1[1] = "い"
arr1[2] = "う"

arr2 = createArray()
arr2[0] = "う"
arr2[1] = "い"
arr2[2] = "あ"

speak(array_comparison(arr1, arr2))
#false

コメントする

コメントするには、ログインする必要があります。

コメント一覧

コメントはありません。