Search the Community
Showing results for tags '1d'.
-
We often have to get a 2D from a 1D array. A good example is with StringRegExp : it can only return a 1D array. But it can be interesting to transform the result to a 2D array. Of course, it's easy to make it, but I think this function could help someone. First, an example : ; #EXAMPLE# ===================================================================================================================== #Include <Array.au3> Local $sString = "<select>" & @CRLF & _ " <option value='volvo'>Volvo</option>" & @CRLF & _ " <option value='saab'>Saab</option>" & @CRLF & _ " <option value='mercedes'>Mercedes</option>" & @CRLF & _ " <option value='audi'>Audi</option>" & @CRLF & _ " <option value='porsche'>Porsche</option>" & @CRLF & _ "</select>" Local $aValues = StringRegExp($sString, "value='([^']+)'>([^<]+)", 3) _ArrayDisplay($aValues, "1D Array") Local $aValues2D = _Array1DTo2D($aValues, 2, 0, 0, 0) _ArrayDisplay($aValues2D, "2D Array") ; =============================================================================================================================== Now, the function : ; #FUNCTION# ==================================================================================================================== ; Name ..........: _Array1DTo2D ; Description ...: Transforms a 1D to a 2D array. ; Syntax ........: _Array1DTo2D($avArray, $iCols[, $iStart = 0[, $iEnd = 0[, $iFlag = 0]]]) ; Parameters ....: $avArray - Array to modify. ; $iCols - Number of columns to transform the array to. ; $iStart - [optional] Index of array to start the transformation. Default is the first element. ; $iEnd - [optional] Index of array to stop the transformation. Default is the last element. ; $iFlag - [optional] If set to 1, the array size must to a multiple of $iCols. Default is 0. ; Return values .: Success : Returns a 2D array ; Failure : Returns 0 and sets @error to : ; 1 - $aArray is not an array ; 2 - $iStart is greater than $iEnd ; 3 - $aArray is not a 1D array ; 4 - $aArray size is not a multiple of $iCols ; Author ........: jguinch ; =============================================================================================================================== Func _Array1DTo2D($avArray, $iCols, $iStart = 0, $iEnd = 0, $iFlag = 0) If $iStart = Default OR $iStart < 0 Then $iStart = 0 If $iEnd = Default Then $iEnd = 0 If NOT IsArray($avArray) Then Return SetError(1, 0, 0) If UBound($avArray, 0) <> 1 Then Return SetError(3, 0, 0) Local $iUBound = UBound($avArray) - 1 If $iEnd < 1 Then $iEnd = $iUBound If $iEnd > $iUBound Then $iEnd = $iUBound If $iStart > $iEnd Then Return SetError(2, 0, 0) Local $iNbRows = ($iEnd - $iStart + 1) / $iCols If $iFlag AND IsFloat($iNbRows) Then Return SetError(2, 0, 0) Local $aRet[ Ceiling($iNbRows) ][$iCols] Local $iCol = 0, $iRow = 0 For $i = $iStart To $iEnd If $iCol = $iCols Then $iCol = 0 $iRow += 1 EndIf $aRet[$iRow][$iCol] = $avArray[$i] $iCol += 1 Next Return $aRet EndFunc I hope it is clear enough
-
Hello all, Summary: I have a basic piece of code that is to be a part of a much larger project; I just can't seem to get the right output. I'm retrieving two lots of powershell data into 2 x 1d arrays and trying to add them into a single 2d array. Retrieving the data together into the 2d array seemed harder, due to the application names varying too much to string split. Data being pulled is application name and GUID. From here I will use this info in a drop down box and an uninstall button to run the required command to remove the selected software (have this sorted already). Problem: When I merge the data it doesn't put the application name and GUID on the same row in differing columns eg. my test box has 24 applications plus some superfluous data from Powershell to be cleaned up by the _arraydeletes. Instead I end up with an array with 58 rows and 2 columns; whereas my temp 1d arrays have 28 rows. As you can see I've tried both _ArrayInsert and _ArrayAdd but I still get the same result. Question: Is there something that I'm doing wrong in putting the data into the 2d array or do I just need to do some more post processing to tidy it up and align the names and GUIDs? Code: #include <Array.au3> $Cmd1 = (" /c Powershell.exe " & Chr(34) & "Get-WmiObject -Class win32reg_addremoveprograms | where {$_.ProdID -like " & Chr(34) & Chr(123) & Chr(42) & Chr(125) & Chr(34) & "} | select DisplayName" & Chr(34)) $Cmd2 = (" /c Powershell.exe " & Chr(34) & "Get-WmiObject -Class win32reg_addremoveprograms | where {$_.ProdID -like " & Chr(34) & Chr(123) & Chr(42) & Chr(125) & Chr(34) & "} | select ProdID" & Chr(34)) Global $aNameGUID[1][2] ;_ArrayDisplay($aNameGUID) ReadApps($Cmd1,0) ;_ArrayDisplay($aNameGUID) ReadApps($Cmd2,1) _ArrayDisplay($aNameGUID) Terminate() Func ReadApps($Command,$col) $DOS = Run(@ComSpec & $Command, "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD) ProcessWaitClose($DOS) $DOSOut = StdoutRead($DOS) ;MsgBox(0,"Data",$DOSOut) ;Show the line items that we want in the array Local $tmpArray = StringSplit(StringTrimRight(StringStripCR($DOSOut), StringLen(@CRLF)), @CRLF) If @error Then MsgBox(0,"FAIL","I failed to find objects") Exit Else _ArrayDisplay($tmpArray) EndIf ;_ArrayDelete($tmpArray, 3) ;_ArrayDelete($tmpArray, 2) ;_ArrayDelete($tmpArray, 1) ;$tmpArray[0] = $tmpArray[0] - 3 For $i = 0 To UBound($tmpArray) - 1 ;_ArrayAdd($aNameGUID, $tmpArray[$i], $col) _ArrayInsert($aNameGUID, 0, $tmpArray[$i], $col) Next $tmpArray = 0 EndFunc ;==>ReadApps While 1 Sleep(1500) WEnd Func Terminate() Exit 0 EndFunc ;==>Terminate Thanks in advance, Luxyboy