Skip to content

Commit f6f8121

Browse files
author
Andreas Tharang
committed
added: _ArrayAddGeneratedColumn()
1 parent 0fd4555 commit f6f8121

File tree

3 files changed

+110
-7
lines changed

3 files changed

+110
-7
lines changed

ArrayPlus.au3

+90-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
; ---- manipulation and conversion ----
2828
; _ArraySlice - python style array slicing to extract ranges, rows, columns, single values
29+
; _ArrayAddGeneratedColumn() - adds generated values as a column (like "generated column" in SQL)
2930
; _Array1DTo2D - convert a 1D-array into a 2D-array and take over the values to the first column (for inverted case - extract a row or column from 2D-array - use _ArraySlice)
3031
; _Array2dToAinA - convert 2D-array into a array-in-array
3132
; _ArrayAinATo2d - convert array-in-array into a 2D array
@@ -640,6 +641,93 @@ Func _ArrayRangeCreate($nStart, $nStop, $nStep = 1, $vDefault = Default)
640641
Return SetExtended($iN, $aRange)
641642
EndFunc ;==>_ArrayRangeCreate
642643

644+
; #FUNCTION# ======================================================================================
645+
; Name ..........: _ArrayAddGeneratedColumn()
646+
; Description ...: Adds a column to a 2D array whose value can be derived from the other columns in the data set
647+
; Syntax ........: _ArrayAddGeneratedColumn(ByRef $aArray, $vCalc, [$iIndex = Ubound($aArray, 2)])
648+
; Parameters ....: $aArray - the 2D array to which the generated column is to be added
649+
; $aB - 2D array or Array-In-Array which should joined with $aA
650+
; $vCalc - Rule for determining the value for the new column
651+
; Can be:
652+
; | user-defined function: calculation rule as a function of the form
653+
; function($a1DArray, $dummy): get the current array row as 1D-Array and calculate the value
654+
; | String: AutoIt-Code as string to calculate the value
655+
; Must contain "$A", where "$A" represents the current array line as a 1D array.
656+
; $iIndex - Column index at which the generated value is to be inserted. Default: at the end of the array
657+
; Return values .: Success: True ($aArray is manipulated directly by reference), @extended = number of rows of $aArray
658+
; Failure: False and set error to:
659+
; | @error = 1 : $aArray is not a 1D/2D array
660+
; | @error = 2 : Invalid value range of $iIndex
661+
; | @error = 3 : no valid form for $vCalc
662+
; Author ........: aspirinjunkie
663+
; Modified ......: 2024-02-12
664+
; Related .......: __ap_cb_getKey_String()
665+
; Example .......: Yes
666+
; Global $aProds[][3] = [["apple", 1.5, "fruits"], ["Pancake", 3.0, "sweets"], ["banana", 2.0, "fruits"], ["banana", 1.0, "plastic"], ["cherry", 3.0, "fruits"]]
667+
; _ArrayAddGeneratedColumn($aProds, "StringLeft($A[0], 1)", 1)
668+
; _ArrayDisplay($aProds)
669+
; =================================================================================================
670+
Func _ArrayAddGeneratedColumn(ByRef $aArray, $vCalc, $iIndex = Ubound($aArray, 2))
671+
Local $bCbIsString = False
672+
Local $nRows = Ubound($aArray, 1)
673+
674+
If Ubound($aArray, 0) = 1 Then
675+
; convert 1D-Array to 2D-Array
676+
Local $aTmp[$nRows][1]
677+
For $i = 0 To $nRows - 1
678+
$aTmp[$i][0] = $aArray[$i]
679+
Next
680+
$aArray = $aTmp
681+
EndIf
682+
683+
If Ubound($aArray, 0) <> 2 Then Return SetError(1, Ubound($aArray, 0), False)
684+
If $iIndex > Ubound($aArray, 2) Or $iIndex < 0 Then Return SetError(2, $iIndex, False)
685+
686+
Local $nCols = Ubound($aArray, 2)
687+
688+
Local $cbKey
689+
Select
690+
Case IsFunc($vCalc) ; user defined function
691+
$cbKey = $vCalc
692+
693+
Case IsString($vCalc) ; function directly as a string
694+
Local $bBefore = Opt("ExpandEnvStrings", 1)
695+
$cbKey = __ap_cb_getKey_String
696+
$bCbIsString = True
697+
698+
Case Else ; no valid form for $vCalc
699+
Return SetError(3, 0, False)
700+
701+
EndSelect
702+
703+
; add column
704+
Redim $aArray[$nRows][$nCols + 1]
705+
706+
; add generated column
707+
Local $vGenerated
708+
For $iR = 0 To $nRows - 1
709+
710+
; first calculate the generated value (row as separate 1D-array needed)
711+
Local $aRow[$nCols]
712+
For $i = 0 To $nCols - 1
713+
$aRow[$i] = $aArray[$iR][$i]
714+
Next
715+
$vGenerated = $cbKey($aRow, $vCalc)
716+
717+
; move columns after $iIndex by 1
718+
For $iC = UBound($aArray, 2) - 2 To $iIndex Step - 1
719+
$aArray[$iR][$iC + 1] = $aArray[$iR][$iC]
720+
Next
721+
722+
; add generated value
723+
$aArray[$iR][$iIndex] = $vGenerated
724+
Next
725+
726+
If $bCbIsString Then Opt("ExpandEnvStrings", $bBefore)
727+
728+
Return SetExtended($nRows, True)
729+
EndFunc
730+
643731
; #FUNCTION# ======================================================================================
644732
; Name ..........: _Array1DTo2D()
645733
; Description ...: convert a 1D-array into a 2D-array and take over the values to the first column
@@ -754,7 +842,7 @@ EndFunc ;==>_ArrayAinATo2d
754842
; defaults to the same value as $aA
755843
; $sJoinType - type of joining (see https://www.w3schools.com/sql/sql_join.asp for explanation)
756844
; on of these:
757-
; | "inner": inner join - Returns records that have matching values in both tables
845+
; | "inner" (default): inner join - Returns records that have matching values in both tables
758846
; | "left" : left (outer) join - Returns all records from the left table, and the matched records from the right table
759847
; | "right": right (outer) join - Returns all records from the right table, and the matched records from the left table
760848
; | "outer" or "full": (full) outer join - Returns all records when there is a match in either left or right table
@@ -2500,7 +2588,7 @@ Func __ap_cb_getKey_Index_Multi(ByRef Const $aA, Const $aInd)
25002588
Return StringTrimRight($sKey, 1)
25012589
EndFunc ;==>__ap_cb_getKey_Index_Multi
25022590

2503-
; helper function for _ArrayJoin() which determines a primary key from a calculation rule as AutoIt code in the string $sCBString
2591+
; helper function for _ArrayJoin() and _ArrayAddGeneratedColumn which determines a primary key from a calculation rule as AutoIt code in the string $sCBString
25042592
Func __ap_cb_getKey_String(ByRef Const $A, Const $sCBSTRING)
25052593
Local $vRet = Execute($sCBSTRING)
25062594
Return SetError(@error, @extended, $vRet)

ArrayPlus_examples.au3

+19-5
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@
166166
; Func __sqSum(ByRef $sum, $val)
167167
; $sum += $val*$val
168168
; EndFunc
169-
#EndRegion _ArrayReduce()
169+
#EndRegion
170170

171-
#Region _ArrayMinMax()()
171+
#Region _ArrayMinMax()
172172
; example 1 - 1D-Array
173173
; Global $aArray1D[30]
174174
; For $i = 0 To 29
@@ -189,7 +189,7 @@
189189
; _ArrayDisplay($aArray2D)
190190
; $aMinMax = _ArrayMinMax($aArray2D)
191191
; _ArrayDisplay($aMinMax)
192-
#endRegion
192+
#EndRegion
193193

194194
#Region _ArrayDeleteByCondition()
195195

@@ -206,7 +206,7 @@
206206
; Local $a_Array = ["BASF", "Allianz", "Volkswagen", "BMW", "Bayer", "Telekom", "Post", "Linde"]
207207
; _ArrayDeleteByCondition($a_Array, 'StringLeft($A, 1) = "B"')
208208
; If Not @error Then _ArrayDisplay($a_Array)
209-
#endRegion
209+
#EndRegion
210210

211211
#Region _ArrayFilter()
212212

@@ -224,7 +224,7 @@
224224
; _ArrayFilter($a_Array, 'StringLeft($A, 1) = "B"')
225225
; If Not @error Then _ArrayDisplay($a_Array)
226226

227-
#endRegion
227+
#EndRegion
228228

229229
#Region _ArrayJoin()
230230

@@ -240,4 +240,18 @@
240240
;~ $aJoin = _ArrayJoin($aProds, $aColors, "StringLeft($A[0], 1)")
241241
;~ _ArrayDisplay($aJoin, "by first letter")
242242

243+
#EndRegion
244+
245+
#Region _ArrayAddGeneratedColumn()
246+
247+
; add column with the first letter of the first column value
248+
;~ Global $aProds[][3] = [["apple", 1.5, "fruits"], ["Pancake", 3.0, "sweets"], ["banana", 2.0, "fruits"], ["banana", 1.0, "plastic"], ["cherry", 3.0, "fruits"]]
249+
;~ _ArrayAddGeneratedColumn($aProds, "StringLeft($A[0], 1)", 1)
250+
;~ _ArrayDisplay($aProds, "first letter")
251+
252+
;~ ; convert 1D-Array to 2D and add a column with random data
253+
;~ Global $aTest[] = ["apple", "pancake", "banana", "cherry", "strawberry", "cucumber"]
254+
;~ _ArrayAddGeneratedColumn($aTest, "Random(1,100, 1)", 1)
255+
;~ _ArrayDisplay($aTest, "new random column")
256+
243257
#EndRegion

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ The function list of the UDF:
1414
|_ArrayRangeCreate() | create a sequence as 1D-array - mainly helper function for _ArrayCreate |
1515
| ***manipulation and conversion*** | |
1616
|_ArraySlice | python style array slicing to extract ranges, rows, columns, single values |
17+
|_ArrayAddGeneratedColumn | adds generated values as a column (like "generated column" in SQL) |
1718
|_Array1DTo2D | convert a 1D-array into a 2D-array and take over the values to the first column (for inverted case - extract a row or column from 2D-array - use _ArraySlice) |
1819
|_Array2dToAinA | convert 2D-array into a array-in-array |
1920
|_ArrayAinATo2d | convert array-in-array into a 2D array |

0 commit comments

Comments
 (0)