UBoundユーバウンド

本ページには広告が含まれています。

配列の上限値(最大インデックス)を返します。arraynameに上限値を求める配列の名前、dimensionに上限値を求める配列の次元を指定します。

構文
  1. Integer = UBound( arrayname, dimension )
引数
arrayname (Array必須
上限値を求める配列の名前
dimension (Integer = 1)省略可
返す次元を示す整数
戻り値
配列の上限値

プログラム

UWSC
//////////////////////////////////////////////////
// 【引数】
//   arrayname : 上限値を求める配列の名前 
//   dimension : 返す次元を示す整数 
// 【戻り値】
//   配列の上限値 
//////////////////////////////////////////////////
FUNCTION UBound(arrayname[], dimension = 1)
	RESULT = EVAL("RESIZE(arrayname" + strRepeat("[0]", dimension - 1) + ")")
FEND

//////////////////////////////////////////////////
// 【引数】
//   inputs : 繰り返す文字列 
//   multiplier : inputsを繰り返す回数 
// 【戻り値】
//   inputsをmultiplier回を繰り返した文字列を返します 
//////////////////////////////////////////////////
FUNCTION strRepeat(inputs, multiplier)
	DIM res = ""
	FOR n = 1 TO multiplier
		res = res + inputs
	NEXT
	RESULT = res
FEND

解説

  1. 2行目
    UWSC
    	RESULT = EVAL("RESIZE(arrayname" + strRepeat("[0]", dimension - 1) + ")")
    arraynameの右側にdimensionの数値から1を引いた数だけ[0]を繰り返した文字列を結合し、RESIZE関数 (スクリプト関数)の引数として渡します。一次元目の上限値はRESIZE(arrayname)、二次元目の上限値はRESIZE(arrayname[0])、三次元目の上限値はRESIZE(arrayname[0][0])で求められます。EVAL関数 (スクリプト関数)で文字列で書かれた式を実行することができます。

使い方

arrayの上限値を求めます。

UWSC
DIM array[5]

PRINT UBound(array)
結果
プレーンテキスト
5

arrayの一次元目と二次元目の上限値を求めます。

UWSC
DIM array[5][8]

PRINT UBound(array)
PRINT UBound(array, 2)
結果
プレーンテキスト
5
8

SAFEARRAY関数 (スクリプト関数)で生成した配列の一次元目と二次元目の上限値を求めます。

UWSC
DIM array = SAFEARRAY(2, 5, 4, 9)

PRINT UBound(array, 1)
PRINT UBound(array, 2)
結果
プレーンテキスト
5
9

プログラム実行例

フォルダ内のファイルを更新日時でソートする

UWSC
DIM Shell = CREATEOLEOBJ("Shell.Application")
DIM Folder = Shell.NameSpace("C:\Program Files (x86)\UWSC")
DIM FolderItems = Folder.Items

HASHTBL iColumn

FOR i = 0 TO 350
	iColumn[Folder.GetDetailsOf(EMPTYPARAM, i)] = i
NEXT

DIM filename[-1]
DIM datetime[-1]

FOR i = 0 TO FolderItems.Count - 1
	DIM FolderItem = FolderItems.Item(i)
	IF FolderItem.isFolder THEN CONTINUE
	arrayPush(filename, Folder.GetDetailsOf(FolderItem,iColumn["名前"]))
	arrayPush(datetime, Folder.GetDetailsOf(FolderItem, iColumn["更新日時"]))
NEXT

QSORT(datetime, QSRT_NATURALA, filename)

FOR i = 0 TO UBound(filename)
	PRINT filename[i] + "<#TAB>" + datetime[i]
NEXT

//////////////////////////////////////////////////
// 【引数】
//   array : 要素を追加する配列(参照引数) 
//   values : 追加する要素をvalue1から指定 
// 【戻り値】
//   処理後の配列の要素の数 
//////////////////////////////////////////////////
FUNCTION arrayPush(var array[], value1 = EMPTY, value2 = EMPTY, value3 = EMPTY, value4 = EMPTY, value5 = EMPTY, value6 = EMPTY, value7 = EMPTY, value8 = EMPTY, value9 = EMPTY, value10 = EMPTY, value11 = EMPTY, value12 = EMPTY, value13 = EMPTY, value14 = EMPTY, value15 = EMPTY, value16 = EMPTY)
    DIM i = 1
    WHILE EVAL("value" + i) <> EMPTY
	  DIM res = RESIZE(array, UBound(array) + 1)
	  array[res] = EVAL("value" + i)
	  i = i + 1
	WEND
	RESULT = LENGTH(array)
FEND

//////////////////////////////////////////////////
// 【引数】
//   inputs : 繰り返す文字列 
//   multiplier : inputsを繰り返す回数 
// 【戻り値】
//   inputsをmultiplier回を繰り返した文字列を返します 
//////////////////////////////////////////////////
FUNCTION strRepeat(inputs, multiplier)
	DIM res = ""
	FOR n = 1 TO multiplier
		res = res + inputs
	NEXT
	RESULT = res
FEND

//////////////////////////////////////////////////
// 【引数】
//   arrayname : 上限値を求める配列の名前 
//   dimension : 返す次元を示す整数 
// 【戻り値】
//   配列の上限値 
//////////////////////////////////////////////////
FUNCTION UBound(arrayname[], dimension = 1)
	RESULT = EVAL("RESIZE(arrayname" + strRepeat("[0]", dimension - 1) + ")")
FEND
結果
プレーンテキスト
UDebug.chm	2014/05/11 1:58
RecIE.chm	2014/05/11 1:58
Uws2Exe.chm	2015/08/13 16:12
RecUws.dll	2016/03/25 4:57
UWSC.exe	2016/10/15 13:33
Uws2Exe.exe	2016/10/15 13:34
RecIE.exe	2016/10/15 13:35
UDebug.exe	2016/10/15 13:37
XRef.exe	2016/10/15 13:38
Readme.txt	2016/10/15 13:39
uwsc.chm	2016/10/15 13:41
unins000.exe	2018/06/27 12:48
unins000.dat	2018/06/27 12:49
使用関数

テキストファイルの内容を行毎にソートして出力する

UWSC
DIM FID = FOPEN("D:\Documents\sample.txt", F_READ)
DIM array[-1]

FOR i = 1 TO FGET(FID, F_LINECOUNT)
	arrayPush(array, FGET(FiD, i))
NEXT

FCLOSE(FID)

QSORT(array, QSRT_A)

FOR i = 0 TO UBound(array)
	PRINT array[i]
NEXT

//////////////////////////////////////////////////
// 【引数】
//   array : 要素を追加する配列(参照引数) 
//   values : 追加する要素をvalue1から指定 
// 【戻り値】
//   処理後の配列の要素の数 
//////////////////////////////////////////////////
FUNCTION arrayPush(var array[], value1 = EMPTY, value2 = EMPTY, value3 = EMPTY, value4 = EMPTY, value5 = EMPTY, value6 = EMPTY, value7 = EMPTY, value8 = EMPTY, value9 = EMPTY, value10 = EMPTY, value11 = EMPTY, value12 = EMPTY, value13 = EMPTY, value14 = EMPTY, value15 = EMPTY, value16 = EMPTY)
    DIM i = 1
    WHILE EVAL("value" + i) <> EMPTY
	  DIM res = RESIZE(array, UBound(array) + 1)
	  array[res] = EVAL("value" + i)
	  i = i + 1
	WEND
	RESULT = LENGTH(array)
FEND

//////////////////////////////////////////////////
// 【引数】
//   inputs : 繰り返す文字列 
//   multiplier : inputsを繰り返す回数 
// 【戻り値】
//   inputsをmultiplier回を繰り返した文字列を返します 
//////////////////////////////////////////////////
FUNCTION strRepeat(inputs, multiplier)
	DIM res = ""
	FOR n = 1 TO multiplier
		res = res + inputs
	NEXT
	RESULT = res
FEND

//////////////////////////////////////////////////
// 【引数】
//   arrayname : 上限値を求める配列の名前 
//   dimension : 返す次元を示す整数 
// 【戻り値】
//   配列の上限値 
//////////////////////////////////////////////////
FUNCTION UBound(arrayname[], dimension = 1)
	RESULT = EVAL("RESIZE(arrayname" + strRepeat("[0]", dimension - 1) + ")")
FEND
使用関数

テキストファイルの内容を行毎にソートして書き換える

UWSC
DIM FID = FOPEN("D:\Documents\sample.txt", F_READ OR F_WRITE)
DIM array[-1]

FOR i = 1 TO FGET(FID, F_LINECOUNT)
	arrayPush(array, FGET(FiD, i))
NEXT

QSORT(array, QSRT_A)

FPUT(FID, "", F_ALLTEXT)

FOR i = 0 TO UBound(array)
	FPUT(FID, array[i], i + 1)
NEXT

FCLOSE(FID)

//////////////////////////////////////////////////
// 【引数】
//   array : 要素を追加する配列(参照引数) 
//   values : 追加する要素をvalue1から指定 
// 【戻り値】
//   処理後の配列の要素の数 
//////////////////////////////////////////////////
FUNCTION arrayPush(var array[], value1 = EMPTY, value2 = EMPTY, value3 = EMPTY, value4 = EMPTY, value5 = EMPTY, value6 = EMPTY, value7 = EMPTY, value8 = EMPTY, value9 = EMPTY, value10 = EMPTY, value11 = EMPTY, value12 = EMPTY, value13 = EMPTY, value14 = EMPTY, value15 = EMPTY, value16 = EMPTY)
    DIM i = 1
    WHILE EVAL("value" + i) <> EMPTY
	  DIM res = RESIZE(array, UBound(array) + 1)
	  array[res] = EVAL("value" + i)
	  i = i + 1
	WEND
	RESULT = LENGTH(array)
FEND

//////////////////////////////////////////////////
// 【引数】
//   inputs : 繰り返す文字列 
//   multiplier : inputsを繰り返す回数 
// 【戻り値】
//   inputsをmultiplier回を繰り返した文字列を返します 
//////////////////////////////////////////////////
FUNCTION strRepeat(inputs, multiplier)
	DIM res = ""
	FOR n = 1 TO multiplier
		res = res + inputs
	NEXT
	RESULT = res
FEND

//////////////////////////////////////////////////
// 【引数】
//   arrayname : 上限値を求める配列の名前 
//   dimension : 返す次元を示す整数 
// 【戻り値】
//   配列の上限値 
//////////////////////////////////////////////////
FUNCTION UBound(arrayname[], dimension = 1)
	RESULT = EVAL("RESIZE(arrayname" + strRepeat("[0]", dimension - 1) + ")")
FEND
使用関数

関連記事

CALCARRAY関数 (スクリプト関数)
配列データを計算します。
GETALLWIN関数 (スクリプト関数)
全ウィンドウのIDを取得します。
GETOLEITEM関数 (スクリプト関数)
コレクションを取得します。
JOIN関数 (スクリプト関数)
配列の中身を区切り文字で結合し、文字列として返します。
LENGTH関数 (スクリプト関数)
文字数もしくは配列サイズを返します。
SETCLEAR関数 (スクリプト関数)
配列を指定された値で埋めます。
SHIFTARRAY関数 (スクリプト関数)
配列データをシフトします。
連想配列
連想配列とは、自動的に割り当てられる数字をキーとして持つかわりに、自由に任意の文字列を割り振ることができる配列のことです。添え字に番号の変わりに名前をつけることでわかりやすく管理することができます。
divisors (自作関数)
引数に指定した数値の約数のリストを返します。
Collatz (自作関数)
コラッツ数列 を求め結果を配列で返します。