|
26 | 26 |
|
27 | 27 | ; ---- manipulation and conversion ----
|
28 | 28 | ; _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) |
29 | 30 | ; _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)
|
30 | 31 | ; _Array2dToAinA - convert 2D-array into a array-in-array
|
31 | 32 | ; _ArrayAinATo2d - convert array-in-array into a 2D array
|
@@ -640,6 +641,93 @@ Func _ArrayRangeCreate($nStart, $nStop, $nStep = 1, $vDefault = Default)
|
640 | 641 | Return SetExtended($iN, $aRange)
|
641 | 642 | EndFunc ;==>_ArrayRangeCreate
|
642 | 643 |
|
| 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 | + |
643 | 731 | ; #FUNCTION# ======================================================================================
|
644 | 732 | ; Name ..........: _Array1DTo2D()
|
645 | 733 | ; Description ...: convert a 1D-array into a 2D-array and take over the values to the first column
|
@@ -754,7 +842,7 @@ EndFunc ;==>_ArrayAinATo2d
|
754 | 842 | ; defaults to the same value as $aA
|
755 | 843 | ; $sJoinType - type of joining (see https://www.w3schools.com/sql/sql_join.asp for explanation)
|
756 | 844 | ; 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 |
758 | 846 | ; | "left" : left (outer) join - Returns all records from the left table, and the matched records from the right table
|
759 | 847 | ; | "right": right (outer) join - Returns all records from the right table, and the matched records from the left table
|
760 | 848 | ; | "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)
|
2500 | 2588 | Return StringTrimRight($sKey, 1)
|
2501 | 2589 | EndFunc ;==>__ap_cb_getKey_Index_Multi
|
2502 | 2590 |
|
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 |
2504 | 2592 | Func __ap_cb_getKey_String(ByRef Const $A, Const $sCBSTRING)
|
2505 | 2593 | Local $vRet = Execute($sCBSTRING)
|
2506 | 2594 | Return SetError(@error, @extended, $vRet)
|
|
0 commit comments