Что нового

[Массивы] _ArrayDisplayConsole() - Вывод массива в консоль

IIuOHeP

Новичок
Сообщения
21
Репутация
2
Периодически возникала необходимость выводить массив в консоль. (Вывод данных без остановки скрипта).
Скрипт писал для себя... вот что получилось:
Код:
Dim $Array0[3] = [5,6,7]
Dim $Array1[3] = [$Array0, 2, 3]
Dim $Array2[2][2] = [[1, 2],[3, 4]]
Dim $Array3[2][2][2] = [[[1, 2],[2, 2]],[[2, 4],[6, 7]]]
Dim $Array4 = 5
Dim $Array5[3][5] = [["1234567890123456789012", $Array1, $Array4, "", 4122], _
		["---------------Tab			", $Array2, "1234567", "Пиво2222", 6122], _
		["V", $Array3, "TT", 6, 8122]]


_ArrayDisplayConsole($Array1, 1, "\")
Sleep(2000)
_ArrayDisplayConsole($Array2, 0)
Sleep(2000)
_ArrayDisplayConsole($Array3)
Sleep(2000)
_ArrayDisplayConsole($Array4)
Sleep(2000)
_ArrayDisplayConsole($Array5)

Func _ArrayDisplayConsole($Array, $ViewStAndCol = 1, $separator = "|") ;(Array 1D and 2D, Show number string and column, separator)
	If Not IsArray($Array) Then ConsoleWrite("! It not $Array" & @CRLF)
	If UBound($Array, 0) > 2 Then ConsoleWrite("! Array > 2D not supported" & @CRLF)

	If UBound($Array, 0) = 1 Then ;Array 1D
		For $index = 0 To UBound($Array) - 1
			if not IsArray($Array[$index]) Then
				If $ViewStAndCol = 0 Then
					ConsoleWrite($Array[$index] & @CRLF)
				Else
					ConsoleWrite('Str_' & $index & @TAB & $separator & $Array[$index] & @CRLF)
				EndIf
			Else
				If $ViewStAndCol = 0 Then
					ConsoleWrite("$Array_" &UBound($Array[$index], 0) & "D" & @CRLF)
				Else
					ConsoleWrite('Str_' & $index & @TAB & "$Array_" &UBound($Array[$index], 0) & "D" & @CRLF)
				EndIf

			EndIf
		Next
	ElseIf UBound($Array, 0) = 2 Then ;Array 2D
		Dim $ArrayStringLen[UBound($Array, 1)][UBound($Array, 2)]
		Dim $ArrayStringLenMax[2][UBound($Array, 2)]

		For $CIndex = 0 To UBound($Array, 2) - 1 Step 1


			$Temp = 0
			$tempSIndex = 0
			$TempSamm = 0
			For $SIndex = 0 To UBound($Array, 1) - 1 Step 1 
				If IsArray($Array[$SIndex][$CIndex]) Then $Array[$SIndex][$CIndex] = "$Array_" & UBound($Array[$SIndex][$CIndex], 0) & "D"
				$Array[$SIndex][$CIndex] = StringReplace($Array[$SIndex][$CIndex], @TAB, ChrW(172))
				If $Temp < StringLen($Array[$SIndex][$CIndex]) Then
					$tempSIndex = $SIndex
					$Temp = StringLen($Array[$SIndex][$CIndex])
				EndIf
				$TempSamm += StringLen($Array[$SIndex][$CIndex])
			Next

			If $Temp = $TempSamm / UBound($Array, 1) Then 
				$ArrayStringLenMax[0][$CIndex] = $Temp
				$ArrayStringLenMax[1][$CIndex] = -1
			Else
				$ArrayStringLenMax[0][$CIndex] = $Temp
				$ArrayStringLenMax[1][$CIndex] = $tempSIndex
			EndIf

			For $SIndex = 0 To UBound($Array, 1) - 1 Step 1

				If $SIndex <> $ArrayStringLenMax[1][$CIndex] Then 

					$difference = ($ArrayStringLenMax[0][$CIndex] + 1) / 8
					$difference2 = $difference - ($ArrayStringLenMax[0][$CIndex] - StringLen($Array[$SIndex][$CIndex])) / 8
					If $difference - Floor($difference) > 0 Then $difference = Floor($difference) + 1
					$difference = $difference - Floor($difference2)

					For $IndexDeff = 0 To $difference - 1 Step 1
						$Array[$SIndex][$CIndex] = $Array[$SIndex][$CIndex] & @TAB
					Next
				Else 
					$StringLen = StringLen($Array[$SIndex][$CIndex]) + 1
					If IsFloat($StringLen / 8) = 0 Then 
					Else
						$Array[$SIndex][$CIndex] = $Array[$SIndex][$CIndex] & @TAB
					EndIf
				EndIf
			Next

		Next

		If $ViewStAndCol = 1 Then
			$Result = ">" & @TAB
			For $SIndex = 0 To UBound($Array, 2) - 1 Step 1
				$Result = $Result & $separator & "Col_" & $SIndex
				$difference = ($ArrayStringLenMax[0][$SIndex] + 1) / 8
				If $difference - Floor($difference) > 0 Then $difference = Floor($difference) + 1
				For $IndexDeff = 0 To $difference - 1 Step 1
					$Result = $Result & @TAB
				Next
			Next
			ConsoleWrite($Result & $separator & @CRLF)
		EndIf

		For $CIndex = 0 To UBound($Array, 1) - 1 Step 1
			If $ViewStAndCol = 0 Then
				$Result = $separator
			Else
				$Result = "Str_" & $CIndex & @TAB & $separator
			EndIf
			For $SIndex = 0 To UBound($Array, 2) - 1 Step 1
				$Result = $Result & $Array[$CIndex][$SIndex] & $separator
			Next
			ConsoleWrite($Result & @CRLF)
		Next

	EndIf

EndFunc   ;==>_ArrayDisplayConsole
 
Верх