-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayPlus.au3
3346 lines (2968 loc) · 136 KB
/
ArrayPlus.au3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include-once
#include <Array.au3>
#include <File.au3>
; #INDEX# =======================================================================================================================
; Title .........: ArrayPlus-UDF
; Version .......: 0.6
; AutoIt Version : 3.3.16.1
; Language ......: english (german maybe by accident)
; Description ...: advanced helpers for array handling
; Author(s) .....: AspirinJunkie
; Last changed ..: 2024-02-12
; Link ..........: https://github.com/Sylvan86/autoit-arrayplus-udf
; License .......: This work is free.
; You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2,
; as published by Sam Hocevar.
; See http://www.wtfpl.net/ for more details.
; ===============================================================================================================================
; #CURRENT# =====================================================================================================================
; ---- creation ------
; _ArrayCreate - create 1D/2D-arrays or Array-In-Arrays in one code-line; supports python-like range-syntax for creating sequences
; _ArrayRangeCreate() - create a sequence as 1D-array - mainly helper function for _ArrayCreate
; ---- manipulation and conversion ----
; _ArraySlice - python style array slicing to extract ranges, rows, columns, single values
; _ArrayAddGeneratedColumn() - adds generated values as a column (like "generated column" in SQL)
; _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)
; _Array2dToAinA - convert 2D-array into a array-in-array
; _ArrayAinATo2d - convert array-in-array into a 2D array
; _Array2String - print a 1D/2D-array to console or variable clearly arranged
; _ArrayAlignDec - align a 1D-array or a column of a 2D-array at the decimal point or right aligned
; _ArrayJoin - sql-like joins for AutoIt-Arrays
; _ArrayMap - apply a function to every element of a array ("map" the function)
; _ArrayReduce - reduce the elements of a array to one value with an external function
; _ArrayFilter - filter the elements of an array with a external function
; _ArrayDeleteByCondition - delete all empty string elements or elements which fulfil a user-defined condition inside an array
; _ArrayDeleteMultiValues - removes elements that appear more than once in the string. (not only the duplicates)
; _ArrayRotate - rotates the elements of a 1D-Array or the rows of a 2D-Array
; ---- sorting ----
; _ArraySortFlexible - sort an array with a user-defined sorting rule
; _ArraySortInsertion - sort an array with a user-defined sorting rule with the insertion-sort algorithm
; _ArraySortSelection - sort an array with a user-defined sorting rule with the selection-sort algorithm (minimal number of swaps)
; _ArrayIsSorted - checks whether an Array is already sorted (by using a user comparison function)
; _ArrayHeapSortBinary - sort an array with Binary-Min-Heap-Sort algorithm (by using a user comparison function)
; _ArrayHeapSortTernary - sort an array with Ternary-Min-Heap-Sort algorithm (by using a user comparison function)
; _ArrayMergeSorted - merges a sorted array or one value into a sorted array so that the sorting is preserved
; ---- searching ----
; _ArrayBinarySearchFlex - performs a binary search for an appropriately sorted array using an individual comparison function
; _ArrayFindSortedPos - find the insertion position of an element in a sorted array.
; _ArrayGetMax - determine the element with the maximum value by using a user comparison function
; _ArrayGetMin - determine the element with the minimum value by using a user comparison function
; _ArrayMinMax - returns min and max value and their indices of a 1D array or all/specific column of a 2D array
; _ArrayGetNthBiggestElement - determine the nth biggest element (e.g.: median value) in an unsorted array without sorting it (faster)
; ===============================================================================================================================
; #INTERNAL_USE_ONLY# ===========================================================================================================
; __ap_cb_comp_Normal
; __ap_cb_comp_Natural
; __ap_cb_comp_String
; __ap_swap
; __ap_PartitionHoare
; __ap_ArrayDualPivotQuicksort
; _2Sort
; __2Sort
; _3Sort
; __3Sort
; _4Sort
; __4Sort
; _5Sort
; __5Sort
; ===============================================================================================================================
; #VARIABLES# ===================================================================================================================
Global $sAPLUS_CBSTRING = "" ; global variable used in __ap_cb_comp_String():
; ===============================================================================================================================
; #CONSTANTS# ===================================================================================================================
Global Enum Step *2 $A2S_BORDERS, $A2S_ALIGNDEC, $A2S_CENTERHEADENTRIES, $A2S_FIRSTROWHEADER, $A2S_TOCONSOLE, $A2S_CHECK_ARRAY_IN_ARRAY
; ===============================================================================================================================
; #FUNCTION# ======================================================================================
; Name ..........: _Array2String()
; Description ...: print a 1D/2D-array to console or variable clearly arranged
; Syntax ........: _Array2String($aArray[, $sHeader = Default[, $cSep = " | "[, $iDecimals = Default[, $dFlags = $A2S_BORDERS + $A2S_ALIGNDEC + $A2S_CENTERHEADENTRIES[, $cRowSep = @CRLF]]]]])
; Parameters ....: $aArray - 1D or 2D array to be printed
; $sHeader - [optional] $sHeader = Default: no header to print (default:Default)
; |$sHeader = True: first row = header values
; |$sHeader = comma separated string: header values
; $cSep - [optional] column separater string (default:" | ")
; $iDecimals - [optional] number of decimal places to round for floating point values (default:Default)
; $dFlags - [optional] Bitmask for serveral options: (default:$A2S_BORDERS + $A2S_ALIGNDEC + $A2S_CENTERHEADENTRIES)
; |(1) $A2S_BORDERS - print table borders
; |(2) $A2S_ALIGNDEC - align numbers at the decimal point
; |(4) $A2S_CENTERHEADENTRIES - header entries are centered instead of right aligned
; |(8) $A2S_FIRSTROWHEADER - first row = header (concurrenct with $sHeader = True)
; |(16) $A2S_TOCONSOLE - table is also printed to console
; |(32) $A2S_CHECK_ARRAY_IN_ARRAY - table is also printed to console
; $cRowSep - [optional] row separator string (default:@CRLF)
; Return values .: Success: the string form of the array
; Failure
; Author ........: aspirinjunkie
; Modified ......: 2022-06-20
; Related .......: __ap_stringCenter, _ArrayAlignDec
; Example .......: Yes
; Global $aCSVRaw[5][4] = [[1, 2, 20.65, 3], [4, 5, 4.1, 6], [7, 8, 111111111.8, 9], [10, 11, 100.2, 12], [13, 14, 23.765, 15]]
; ConsoleWrite(_Array2String($aCSVRaw, "Col. 1, Col. 2, Col. 3, Col. 4"))
; =================================================================================================
Func _Array2String($aArray, $sHeader = Default, $cSep = " | ", $iDecimals = Default, $dFlags = $A2S_BORDERS + $A2S_ALIGNDEC + $A2S_CENTERHEADENTRIES, $cRowSep = @CRLF)
Local $nR = UBound($aArray, 1), $sOut = ""
; if option is set, then check if array is a array-in-array and convert to 2D-array
If BitAND($dFlags, $A2S_CHECK_ARRAY_IN_ARRAY) And UBound($aArray, 0) = 1 Then
; check if array is a array-in-array
Local $bAInA = False
For $i = 0 To UBound($aArray) - 1
If IsArray($aArray[$i]) Then
$bAInA = True
ExitLoop
EndIf
Next
If $bAInA Then $aArray = _ArrayAinATo2d($aArray)
EndIf
Local $nC = UBound($aArray, 2)
If UBound($aArray, 0) = 1 Then ; 1D-array
If BitAND($dFlags, $A2S_ALIGNDEC) Then _ArrayAlignDec($aArray)
For $i = 0 To UBound($aArray) - 1
$sOut &= $aArray[$i] & $cRowSep
Next
Else ; 2D-array
Local $aSizes[$nC], $vTemp, $aTmp
; determine column widths
If BitAND($dFlags, $A2S_ALIGNDEC) Then
For $iC = 0 To $nC - 1
$aSizes[$iC] = _ArrayAlignDec($aArray, $iC, $iDecimals)
Next
Else
For $iC = 0 To $nC - 1
For $iR = 0 To $nR - 1
$vTemp = (IsFloat($aArray[$iR][$iC]) And $iDecimals <> Default) ? StringFormat("%." & $iDecimals & "f", $aArray[$iR][$iC]) : $aArray[$iR][$iC]
If StringLen($vTemp) > $aSizes[$iC] Then $aSizes[$iC] = StringLen($vTemp)
Next
Next
EndIf
If $sHeader <> Default Then ; header treatment
Local $aHeader
If (IsBool($sHeader) And $sHeader = True) Or BitAND($dFlags, $A2S_FIRSTROWHEADER) Then ; first row = header row
$dFlags = BitOR($dFlags, $A2S_FIRSTROWHEADER)
$aHeader = _ArraySlice($aArray, "[0][:]")
Else
$aHeader = StringRegExp($sHeader, '\h*([^,]*[^\h,])', 3)
EndIf
If UBound($aHeader) <> $nC Then Return SetError(1, UBound($aHeader), Null)
For $iH = 0 To UBound($aHeader) - 1
If StringLen($aHeader[$iH]) > $aSizes[$iH] Then $aSizes[$iH] = StringLen($aHeader[$iH])
$sOut &= BitAND($dFlags, $A2S_CENTERHEADENTRIES) ? _
__ap_stringCenter($aHeader[$iH], $aSizes[$iH]) & ($iH = $nC - 1 ? "" : $cSep) : _
StringFormat("% " & $aSizes[$iH] & "s", $aHeader[$iH]) & ($iH = $nC - 1 ? "" : $cSep)
Next
$sOut &= @CRLF
; header seperator
For $iC = 0 To $nC - 1
For $i = 1 To $aSizes[$iC] + ($iC = $nC - 1 ? 0 : StringLen($cSep))
$sOut &= "-"
Next
Next
$sOut &= @CRLF
EndIf
; print data
For $iR = (BitAND($dFlags, $A2S_FIRSTROWHEADER) ? 1 : 0) To $nR - 1
For $iC = 0 To $nC - 1
If BitAND($dFlags, $A2S_ALIGNDEC) Then
$sOut &= StringFormat("% " & $aSizes[$iC] & "s", $aArray[$iR][$iC]) & ($iC = $nC - 1 ? "" : $cSep)
Else
$sOut &= StringFormat("%" & $aSizes[$iC] & "s", $aArray[$iR][$iC]) & ($iC = $nC - 1 ? "" : $cSep)
EndIf
Next
$sOut &= @CRLF
Next
; lower border
If BitAND($dFlags, $A2S_BORDERS) Then
Local $sBorder = ""
For $iC = 0 To $nC - 1
For $i = 1 To $aSizes[$iC] + ($iC = $nC - 1 ? 0 : StringLen($cSep))
$sBorder &= "="
Next
Next
$sOut = $sBorder & @CRLF & $sOut & $sBorder & @CRLF
EndIf
EndIf
If BitAND($dFlags, $A2S_TOCONSOLE) Then ConsoleWrite($sOut)
Return $sOut
EndFunc ;==>_Array2String
; #FUNCTION# ======================================================================================
; Name ..........: _ArrayAlignDec()
; Description ...: align a 1D-array or a column of a 2D-array at the decimal point or right aligned
; Syntax ........: _ArrayAlignDec(ByRef $aArray[, $iColumn = Default[, $iDecimals = Default[, $bTrailingZeros = False]]])
; Parameters ....: ByRef $aArray - 1D or 2D array which values should be char-wise aligned
; |values processed directly in-place
; $iColumn - [optional] If $aArray = 2D-array: column which should be processed (default:Default)
; $iDecimals - [optional] number of decimal places to round for floating point values (default:Default)
; $bTrailingZeros - [optional] If true: add trailing zeros to decimal part if necessary (default:False)
; Return values .: Success: Return calculated column width; @extended = number of decimal digits
; Failure
; Author ........: aspirinjunkie
; Modified ......: 2022-06-20
; Remarks .......: every array element is converted into a string type
; Example .......: Yes
; Local $aArray[] = [20.65, 4.1, 9999999, 10000.2, 100.2, 'Test', 23.765]
; _ArrayAlignDec($aArray)
; For $sElement in $aArray
; ConsoleWrite($sElement & @CRLF)
; Next
; =================================================================================================
Func _ArrayAlignDec(ByRef $aArray, $iColumn = Default, $iDecimals = Default, $bTrailingZeros = False)
Local $aParts, $nN = UBound($aArray), $nMaxReal, $nMaxDec = 0, $nMaxCol, $vVal, $nReal, $nDec
; determine sizes:
For $i = 0 To $nN - 1
$vVal = ($iColumn = Default) ? $aArray[$i] : $aArray[$i][$iColumn]
If IsFloat($vVal) Then
$aParts = StringSplit($vVal, ".", 3)
If StringLen($aParts[0]) > $nMaxReal Then $nMaxReal = StringLen($aParts[0])
If StringLen($aParts[1]) > $nMaxDec Then $nMaxDec = StringLen($aParts[1])
ElseIf IsInt($vVal) Then
If $nMaxReal < StringLen($vVal) Then $nMaxReal = StringLen($vVal)
If $nMaxCol < StringLen($vVal) Then $nMaxCol = StringLen($vVal)
Else
If $nMaxCol < StringLen($vVal) Then $nMaxCol = StringLen($vVal)
EndIf
Next
If ($iDecimals <> Default) And ($iDecimals < $nMaxDec) Then $nMaxDec = $iDecimals
If $nMaxDec > 0 And ($nMaxCol < ($nMaxReal + 1 + $nMaxDec)) Then $nMaxCol = $nMaxReal + 1 + $nMaxDec
If $nMaxDec > 0 And ($nMaxCol > ($nMaxReal + 1 + $nMaxDec)) Then $nMaxReal = $nMaxCol - 1 - $nMaxDec
For $i = 0 To $nN - 1
$vVal = ($iColumn = Default) ? $aArray[$i] : $aArray[$i][$iColumn]
If IsFloat($vVal) Then
$vVal = StringFormat("%" & $nMaxReal + 1 + $nMaxDec & "." & $nMaxDec & "f", $vVal)
If Not $bTrailingZeros Then $vVal = StringRegExpReplace($vVal, '(0(?=0*$))', ' ')
If $iColumn = Default Then
$aArray[$i] = $vVal
Else
$aArray[$i][$iColumn] = $vVal
EndIf
ElseIf IsInt($vVal) And $nMaxDec > 0 Then
$vVal = StringFormat("% " & $nMaxReal & "s%-" & $nMaxDec + 1 & "s", $vVal, " ")
If $iColumn = Default Then
$aArray[$i] = $vVal
Else
$aArray[$i][$iColumn] = $vVal
EndIf
Else
$vVal = StringFormat("% " & $nMaxCol & "s", $vVal)
If $iColumn = Default Then
$aArray[$i] = $vVal
Else
$aArray[$i][$iColumn] = $vVal
EndIf
EndIf
Next
Return SetExtended($nMaxDec, $nMaxCol)
EndFunc ;==>_ArrayAlignDec
; #FUNCTION# ======================================================================================
; Name ..........: _ArraySlice()
; Description ...: python style array slicing to extract ranges, rows, columns, single values
; Syntax ........: _ArraySlice(Const ByRef $aArray, Const $sSliceString)
; Parameters ....: Const ByRef $aArray - 1D/2D input array to be sliced
; Const $sSliceString - slice logic with basic form "[x]" or "x" for 1D-array and "[x][y]" for 2D-array where x/y could be one of these:
; |start - single value/row/column
; |start:end - range definition from-to
; |start:end:step - every step element in range start:end
; |start/end/step are optional so to only turn order of values for example: [::-1]
; |
; |if start/end = negative - counting backwards from the end
; |if step = negative - value order is inverted
; |
; |comma separated integer list - return only specific values/rows/columns
; |
; |single value: return scalar variable for 1D array, 1D-array with the chosen row/column for 2D-arrays - you can extract single rows or columns e.g.: [:][2] or [3][:]
; Return values .: Success: return scalar variable / 1D-array or 2D-array
; Failure: null and set error to:
; |@error = 1: $aArray != 1D/2D-array
; |@error = 2: invalid value in $sSliceString (first dimension group)
; |@error = 3: invalid value in $sSliceString (second dimension group)
; Author ........: aspirinjunkie
; Modified ......: 2022-06-20
; Example .......: Yes
; Global $aCSVRaw[5][3] = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15]]
; ; return 3 specific rows and inverted column order:
; $aSliced = _ArraySlice($aCSVRaw, "[1,3,4][::-1]")
; _ArrayDisplay($aSliced)
; =================================================================================================
Func _ArraySlice(Const ByRef $aArray, Const $sSliceString)
Local $nDims = UBound($aArray, 0), _
$nN1 = UBound($aArray, 1), _
$nN2 = UBound($aArray, 2)
Local $aDimGroups = StringRegExp($sSliceString, '^\s*(?>\[([^\[]*)\]\s*(?>\[([^\[]*)\])?|\[?([^\[]*)\]?\s*$)\s*$', 3)
Local $nGroups, $sGroup1, $sGroup2
Switch UBound($aDimGroups)
Case 1
$nGroups = 1
$sGroup1 = $aDimGroups[0]
Case 2
$nGroups = 2
$sGroup1 = $aDimGroups[0]
$sGroup2 = $aDimGroups[1]
Case 3
$nGroups = 1
$sGroup1 = $aDimGroups[2]
Case Else
Return SetError(1, UBound($aDimGroups), Null)
EndSwitch
Local $iStart1, $iStop1, $iStep1, $iStart2, $iStop2, $iStep2, $aIndices1, $aIndices2
; process first dimension group:
Local $aRE = StringRegExp($sGroup1, '(?x)^\s*(\-?\d+|^\s*(?=:))(?>\:(\-?\d+|(?<=:)))?(?>\:(\-?\d+|(?<=:)))?\s*$', 3)
If @error Then
If Not StringRegExp($sGroup1, '^\s*(\-?\d+)(?:\s*\,\s*\-?\d+)*\s*$') Then Return SetError(2, @error, Null) ; indices list notation
$aIndices1 = StringSplit($sGroup1, ',', 3)
For $i = 0 To UBound($aIndices1) - 1
$aIndices1[$i] = Number($aIndices1[$i])
If $aIndices1[$i] < 0 Then $aIndices1[$i] += $nN1
If $aIndices1[$i] >= $nN1 Then Return SetError(3, $aIndices1[$i], Null)
Next
Else ; range notation
$iStep1 = ((UBound($aRE)) < 3 Or ($aRE[2] == "")) ? 1 : Number($aRE[2])
$iStart1 = ($aRE[0] == "") ? ($iStep1 < 0 ? $nN1 - 1 : 0) : Number($aRE[0]) ; with case check for negative step
$iStop1 = (UBound($aRE) < 2) ? Null : ($aRE[1] == "") ? ($iStep1 < 0 ? 0 : $nN1 - 1) : Number($aRE[1])
EndIf
; process second dimension group:
If $nGroups = 2 Then
$aRE = StringRegExp($sGroup2, '(?x)^\s*(\-?\d+|^\s*(?=:))(?>\:(\-?\d+|(?<=:)))?(?>\:(\-?\d+|(?<=:)))?\s*$', 3)
If @error Then
If Not StringRegExp($sGroup2, '^\s*(\-?\d+)(?:\s*\,\s*\-?\d+)*\s*$') Then Return SetError(3, @error, Null) ; indices list notation
$aIndices2 = StringSplit($sGroup2, ',', 3)
For $i = 0 To UBound($aIndices2) - 1
$aIndices2[$i] = Number($aIndices2[$i])
If $aIndices2[$i] < 0 Then $aIndices2[$i] += $nN2
If $aIndices2[$i] >= $nN2 Then Return SetError(3, $aIndices2[$i], Null)
Next
Else ; range notation
$iStep2 = ((UBound($aRE)) < 3 Or ($aRE[2] == "")) ? 1 : Number($aRE[2])
$iStart2 = ($aRE[0] == "") ? ($iStep2 < 0 ? $nN2 - 1 : 0) : Number($aRE[0]) ; with case check for negative step
$iStop2 = (UBound($aRE) < 2) ? Null : ($aRE[1] == "") ? ($iStep2 < 0 ? 0 : $nN2 - 1) : Number($aRE[1])
EndIf
EndIf
If $iStart1 < 0 Then $iStart1 += $nN1
If $iStop1 < 0 Then $iStop1 += $nN1 - 1
If $iStart2 < 0 Then $iStart2 += $nN2
If $iStop2 < 0 Then $iStop2 += $nN2 - 1
Local $iN1 = ($iStop1 - $iStart1) / $iStep1 + 1, _
$iN2 = ($iStop2 - $iStart2) / $iStep2 + 1
Switch UBound($aArray, 0)
Case 1 ; 1D-Array
If IsArray($aIndices1) Then
Local $aRet[UBound($aIndices1)], $iC = 0
For $i In $aIndices1
$aRet[$iC] = $aArray[$i]
$iC += 1
Next
Return SetExtended(UBound($aRet), $aRet)
Else
Local $aRet[$iN1], $iC = 0
For $i = $iStart1 To $iStop1 Step $iStep1
$aRet[$iC] = $aArray[$i]
$iC += 1
Next
Return SetExtended($iN1, $aRet)
EndIf
Case 2 ; 2D-Array
; check for special cases
Select
Case (IsKeyword($iStop1) = 2) And (IsKeyword($iStop2) = 2) ; a single value
Return $aArray[$iStart1][$iStart2]
Case IsKeyword($iStop1) = 2 ; a single row
Local $aRet[$iN2], $iC = 0
For $i = $iStart2 To $iStop2 Step $iStep2
$aRet[$iC] = $aArray[$iStart1][$i]
$iC += 1
Next
Return SetExtended($iN2, $aRet)
Case IsKeyword($iStop2) = 2 ; a single column
If IsArray($aIndices1) Then ; case for list of indices in first dim
Local $aRet[UBound($aIndices1)], $iC = 0
For $iIndex1 In $aIndices1
$aRet[$iC] = $aArray[$iIndex1][$iStart2]
$iC += 1
Next
Return SetExtended(UBound($aRet), $aRet)
Else ; case for array range in first dim
Local $aRet[$iN1], $iC = 0
For $i = $iStart1 To $iStop1 Step $iStep1
$aRet[$iC] = $aArray[$i][$iStart2]
$iC += 1
Next
Return SetExtended($iN1, $aRet)
EndIf
Case Else ; normal 2D slice
Select ; index list in both dimensions
Case IsArray($aIndices2) And IsArray($aIndices1)
Local $aRet[UBound($aIndices1)][UBound($aIndices2)], $iR = 0, $iC
For $iIndex1 In $aIndices1
$iC = 0
For $iIndex2 In $aIndices2
$aRet[$iR][$iC] = $aArray[$iIndex1][$iIndex2]
$iC += 1
Next
$iR += 1
Next
Return SetExtended(UBound($aRet), $aRet)
; index list in dimension 1
Case IsArray($aIndices1)
Local $aRet[UBound($aIndices1)][$iN2], $iR = 0, $iC
For $iIndex1 In $aIndices1
$iC = 0
For $j = $iStart2 To $iStop2 Step $iStep2
$aRet[$iR][$iC] = $aArray[$iIndex1][$j]
$iC += 1
Next
$iR += 1
Next
Return SetExtended(UBound($aRet), $aRet)
; index list in dimension 2
Case IsArray($aIndices2)
Local $aRet[$iN1][UBound($aIndices2)], $iR = 0, $iC
For $i = $iStart1 To $iStop1 Step $iStep1
$iC = 0
For $iIndex2 In $aIndices2
$aRet[$iR][$iC] = $aArray[$i][$iIndex2]
$iC += 1
Next
$iR += 1
Next
Return SetExtended($iN1, $aRet)
; range definition only
Case Else
Local $aRet[$iN1][$iN2], $iR = 0, $iC
For $i = $iStart1 To $iStop1 Step $iStep1
$iC = 0
For $j = $iStart2 To $iStop2 Step $iStep2
$aRet[$iR][$iC] = $aArray[$i][$j]
$iC += 1
Next
$iR += 1
Next
Return SetExtended($iN1, $aRet)
EndSelect
EndSelect
EndSwitch
EndFunc ;==>_ArraySlice
; #FUNCTION# ======================================================================================
; Name ..........: _ArrayCreate()
; Description ...: create 1D/2D-arrays or Array-In-Arrays in one code-line; supports python-like range-syntax for creating sequences
; Syntax ........: _ArrayCreate($sArrayDef[, $vDefault = Default[, $bArrayInArray = False]])
; Parameters ....: $sArrayDef - String with either a normal AutoIt-Array definition syntax
; |or a "start:stop:step"-syntax like Pythons "range"-command
; |border chars: "[" for inclusive borders and "(" for exclusive borders - example: "(0:4]" --> [1,2,3,4]; "[0:4)" --> [0,1,2,3]
; |no border char defaults to inclusive border
; |step size delimiter can be ":" for a step size and "|" for the number of steps - example "1:5:2" --> [1,3,5]; "1:5|3" --> [1,3,5]
; $vDefault - [optional] If $sArrayDef = range syntax: (default:Default)
; |$vDefault = variant type: default value for array elements
; |$vDefault = Function: firstly sequence is build as defined in $sArrayDef, then this value is passed to this function and overwrite value in the array element (see example)
; If string then the value is parsed as AutoIt-Code. The variable name for the current element should be named "$A" inside the code
; $bArrayInArray - [optional] if True and $sArrayDef is a AutoIt 2D-array definition, then a array-in-array is created instead (default:False)
; Return values .: Success: the arrayy
; Failure: Null and set error to:
; |@error = 1: invalid value in $sArrayDef
; |@error = 2: invald value in $sArrayDef (mixed)
; Author ........: aspirinjunkie
; Modified ......: 2022-07-12
; Remarks .......: useful to create arrays directly in function parameters
; Example .......: Yes
; _ArrayDisplay(_ArrayCreate("2:20:0.5", sqrt))
; _ArrayDisplay(_ArrayCreate("[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15]]"))
; =================================================================================================
Func _ArrayCreate($sArrayDef, $vDefault = Default, $bArrayInArray = False)
Local $aRE = StringRegExp($sArrayDef, '(?x)^\s*[\[\(]?\s*(\-? (?:0|[1-9]\d*) (?:\.\d+)? (?:[eE][-+]?\d+)? | ):(?>(\-? (?:0|[1-9]\d*) (?:\.\d+)? (?:[eE][-+]?\d+)? | ))?(?>[:\|](\-? (?:0|[1-9]\d*) (?:\.\d+)? (?:[eE][-+]?\d+)? | ))?\s*[\]\)]?\s*$', 3)
If Not @error Then ; Array-range
Local $nRE = UBound($aRE)
Local $iStart = ($nRE < 1 Or $aRE[0] == "") ? 0 : Number($aRE[0])
Local $iStop = ($nRE < 2 Or $aRE[1] == "") ? 0 : Number($aRE[1])
Local $iStep = ($nRE < 3 Or $aRE[2] == "") ? 1 : Number($aRE[2])
If StringInStr($sArrayDef, "|", 1) Then ; number of steps instead of step size
Local $nSteps = $iStep
If Not StringIsDigit($nSteps) Or $nSteps < 1 Then Return SetError(3, $nSteps, Null)
$iStep = ($iStop - $iStart) / ($nSteps - 1 + (StringInStr($sArrayDef, "(", 1) ? 1 : 0) + (StringInStr($sArrayDef, ")", 1) ? 1 : 0))
EndIf
; handle exclusive range borders
If StringInStr($sArrayDef, "(", 1) Then $iStart += $iStep
If StringInStr($sArrayDef, ")", 1) Then $iStop -= $iStep
Return _ArrayRangeCreate($iStart, $iStop, $iStep, $vDefault)
Else
$sArrayDef = StringRegExpReplace($sArrayDef, '(^\s*\[|\]\s*$)', '')
Local $aVals = StringRegExp($sArrayDef, '(?sx)(?(DEFINE) (?<string> ''(?>[^'']+|'''')*'' | "(?>[^"]+|"")*") (?<value> \s*(?>\g<string> | [^,\[\]]+)\s* ) (?<subarray> \s*\K\[ \g<value>(?:, \g<value>)* \]) (?<outervalue> \g<subarray> | \g<value> ))\g<outervalue>', 3)
If @error Then Return SetError(1, @error, Null)
Local $bAllSubArray = True, $bAllScalars = True
Local $n2Dim = 0
For $i = 0 To UBound($aVals) - 1
If StringRegExp($aVals[$i], '^\s*\[') Then
$bAllScalars = False
; count elements of second dimension:
StringRegExpReplace($aVals[$i], '(?sx)(?(DEFINE) (?<string> ''(?>[^'']+|'''')*'' | "(?>[^"]+|"")*") (?<value> \s*(?>\g<string> | [^,\[\]]+)\s* ))\g<value>', '')
If @extended > $n2Dim Then $n2Dim = @extended
Else ; scalar value
$bAllSubArray = False
EndIf
Next
; If not Array-In-Array (=1D or 2D-Array) and subvalues are mixed array and scalars then error
If ($bArrayInArray = False) And Not BitXOR($bAllSubArray, $bAllScalars) Then Return SetError(2, 0, Null)
If ($n2Dim > 0) And (Not $bArrayInArray) Then ; 2D-array
Local $aRet[UBound($aVals)][$n2Dim], $aSubVals
For $i = 0 To UBound($aVals) - 1
$aSubVals = StringRegExp($aVals[$i], '(?sx)(?(DEFINE) (?<string> ''(?>[^'']+|'''')*'' | "(?>[^"]+|"")*") (?<value> \s*(?>\g<string> | [^,\[\]]+)\s* ))\g<value>', 3)
For $j = 0 To UBound($aSubVals) - 1
$aRet[$i][$j] = Execute($aSubVals[$j])
Next
Next
Return SetExtended(UBound($aRet), $aRet)
Else ; 1D-Array or array-in-array
Local $aRet[UBound($aVals)]
For $i = 0 To UBound($aVals) - 1
If StringRegExp($aVals[$i], '^\s*\[') Then
$aRet[$i] = _ArrayCreate($aVals[$i], $vDefault, True)
Else
$aRet[$i] = Execute($aVals[$i])
EndIf
Next
Return SetExtended(UBound($aRet), $aRet)
EndIf
EndIf
EndFunc ;==>_ArrayCreate
; #FUNCTION# ======================================================================================
; Name ..........: _ArrayRangeCreate()
; Description ...: create a sequence as 1D-array - mainly helper function for _ArrayCreate
; Syntax ........: _ArrayRangeCreate(Const $nStart, Const $nStop, Const[ $nStep = 1, Const[ $vDefault = Default]])
; Parameters ....: Const $nStart - first value of sequence
; Const $nStop - max value of sequence
; Const $nStep - [optional] step between to values (default:1)
; Const $vDefault - [optional] $vDefault = variant type: default value for array elements (default:Default)
; |$vDefault = Function: firstly sequence is build as defined in $sArrayDef, then this value is passed to this function and overwrite value in the array element (see example)
; Return values .: Success: the sequence as error
; Failure: Null and set @error to:
; |@error = 1: $nStart is not a number
; |@error = 2: $nStop is not a number
; |@error = 3: $nStep is not a number
; Author ........: aspirinjunkie
; Modified ......: 2022-06-22
; Example .......: Yes
; #include <Array.au3>
; _ArrayDisplay(_ArrayRangeCreate(5,24, 2))
; =================================================================================================
Func _ArrayRangeCreate($nStart, $nStop, $nStep = 1, $vDefault = Default)
If Not IsNumber($nStart) Then Return SetError(1, 0, Null)
If Not IsNumber($nStop) Then Return SetError(2, 0, Null)
If Not IsNumber($nStep) Then Return SetError(3, 0, Null)
Local $bCbIsString = False
If StringInStr($vDefault, "$A") Then ; custom filter function directly as a string
Local $bBefore = Opt("ExpandEnvStrings", 1)
$sAPLUS_CBSTRING = $vDefault
$vDefault = __ap_cb_comp_String
$bCbIsString = True
EndIf
Local $iN = ($nStop - $nStart) / $nStep + 1
Local $aRange[$iN] = [(($vDefault = Default) ? $nStart : (IsFunc($vDefault) ? $vDefault($nStart) : $vDefault))]
Local $nCurrent = $nStart
For $i = 1 To $iN - 1
$nCurrent += $nStep
$aRange[$i] = $vDefault = Default ? $nCurrent : (IsFunc($vDefault) ? $vDefault($nCurrent) : $vDefault)
Next
If $bCbIsString Then Opt("ExpandEnvStrings", $bBefore)
Return SetExtended($iN, $aRange)
EndFunc ;==>_ArrayRangeCreate
; #FUNCTION# ======================================================================================
; Name ..........: _ArrayAddGeneratedColumn()
; Description ...: Adds a column to a 2D array whose value can be derived from the other columns in the data set
; Syntax ........: _ArrayAddGeneratedColumn(ByRef $aArray, $vCalc, [$iIndex = Ubound($aArray, 2)])
; Parameters ....: $aArray - the 2D array to which the generated column is to be added
; $aB - 2D array or Array-In-Array which should joined with $aA
; $vCalc - Rule for determining the value for the new column
; Can be:
; | user-defined function: calculation rule as a function of the form
; function($a1DArray, $dummy): get the current array row as 1D-Array and calculate the value
; | String: AutoIt-Code as string to calculate the value
; Must contain "$A", where "$A" represents the current array line as a 1D array.
; $iIndex - Column index at which the generated value is to be inserted. Default: at the end of the array
; Return values .: Success: True ($aArray is manipulated directly by reference), @extended = number of rows of $aArray
; Failure: False and set error to:
; | @error = 1 : $aArray is not a 1D/2D array
; | @error = 2 : Invalid value range of $iIndex
; | @error = 3 : no valid form for $vCalc
; Author ........: aspirinjunkie
; Modified ......: 2024-02-12
; Related .......: __ap_cb_getKey_String()
; Example .......: Yes
; Global $aProds[][3] = [["apple", 1.5, "fruits"], ["Pancake", 3.0, "sweets"], ["banana", 2.0, "fruits"], ["banana", 1.0, "plastic"], ["cherry", 3.0, "fruits"]]
; _ArrayAddGeneratedColumn($aProds, "StringLeft($A[0], 1)", 1)
; _ArrayDisplay($aProds)
; =================================================================================================
Func _ArrayAddGeneratedColumn(ByRef $aArray, $vCalc, $iIndex = Ubound($aArray, 2))
Local $bCbIsString = False
Local $nRows = Ubound($aArray, 1)
If Ubound($aArray, 0) = 1 Then
; convert 1D-Array to 2D-Array
Local $aTmp[$nRows][1]
For $i = 0 To $nRows - 1
$aTmp[$i][0] = $aArray[$i]
Next
$aArray = $aTmp
EndIf
If Ubound($aArray, 0) <> 2 Then Return SetError(1, Ubound($aArray, 0), False)
If $iIndex > Ubound($aArray, 2) Or $iIndex < 0 Then Return SetError(2, $iIndex, False)
Local $nCols = Ubound($aArray, 2)
Local $cbKey
Select
Case IsFunc($vCalc) ; user defined function
$cbKey = $vCalc
Case IsString($vCalc) ; function directly as a string
Local $bBefore = Opt("ExpandEnvStrings", 1)
$cbKey = __ap_cb_getKey_String
$bCbIsString = True
Case Else ; no valid form for $vCalc
Return SetError(3, 0, False)
EndSelect
; add column
Redim $aArray[$nRows][$nCols + 1]
; add generated column
Local $vGenerated
For $iR = 0 To $nRows - 1
; first calculate the generated value (row as separate 1D-array needed)
Local $aRow[$nCols]
For $i = 0 To $nCols - 1
$aRow[$i] = $aArray[$iR][$i]
Next
$vGenerated = $cbKey($aRow, $vCalc)
; move columns after $iIndex by 1
For $iC = UBound($aArray, 2) - 2 To $iIndex Step - 1
$aArray[$iR][$iC + 1] = $aArray[$iR][$iC]
Next
; add generated value
$aArray[$iR][$iIndex] = $vGenerated
Next
If $bCbIsString Then Opt("ExpandEnvStrings", $bBefore)
Return SetExtended($nRows, True)
EndFunc
; #FUNCTION# ======================================================================================
; Name ..........: _Array1DTo2D()
; Description ...: convert a 1D-array into a 2D-array and take over the values to the first column
; Syntax ........: _Array1DTo2D(ByRef $aArray, Const $nCols[, $bInPlace = True])
; Parameters ....: ByRef $aArray - 1D input array
; Const $nCols - number of columns (size of 2nd dimension) in the target 2D-array
; $bInPlace - [optional] If True: overwrite $aArray (default:True)
; |If False: return 2D-array and leave $aArray as is
; Return values .: Success
; |$bInPlace = False: True
; |$bInPlace = True: the result 2D-array
; Failure: Null and set @error
; Author ........: aspirinjunkie
; Modified ......: 2022-06-27
; Remarks .......: for inverted case - extract a column from 2D-array - use _ArraySlice($Array, "[:][N]")
; Example .......: Yes
; Global $aArray[] = [1,2,3,4,5]
; _Array1DTo2D($aArray, 3)
; _ArrayDisplay($aArray)
; =================================================================================================
Func _Array1DTo2D(ByRef $aArray, Const $nCols, $bInPlace = True)
If $nCols < 1 Then Return SetError(1, $nCols, Null)
Local $nElems = UBound($aArray)
Local $aTmp[$nElems][$nCols]
For $i = 0 To $nElems - 1
$aTmp[$i][0] = $aArray[$i]
Next
If $bInPlace Then
$aArray = $aTmp
Return SetExtended($nElems, True)
Else
Return SetExtended($nElems, $aTmp)
EndIf
EndFunc ;==>_Array1DTo2D
; #FUNCTION# ======================================================================================
; Name ..........: _Array2dToAinA()
; Description ...: Convert a 2D array into a Arrays in Array
; Syntax ........: _Array2dToAinA(ByRef $A)
; Parameters ....: $A - the 2D-Array which should be converted
; Return values .: Success: a Arrays in Array build from the input array
; Failure: False
; @error = 1: $A is'nt an 2D array
; Author ........: AspirinJunkie
; =================================================================================================
Func _Array2dToAinA(ByRef $A)
If UBound($A, 0) <> 2 Then Return SetError(1, UBound($A, 0), False)
Local $N = UBound($A), $u = UBound($A, 2)
Local $a_Ret[$N]
For $i = 0 To $N - 1
Local $t[$u]
For $j = 0 To $u - 1
$t[$j] = $A[$i][$j]
Next
$a_Ret[$i] = $t
Next
Return SetExtended($N, $a_Ret)
EndFunc ;==>_Array2dToAinA
; #FUNCTION# ======================================================================================
; Name ..........: _ArrayAinATo2d()
; Description ...: Convert a Arrays in Array into a 2D array
; Syntax ........: _ArrayAinATo2d(ByRef $A)
; Parameters ....: $A - the arrays in array which should be converted
; Return values .: Success: a 2D Array build from the input array
; Failure: Null
; @error = 1: $A is'nt an 1D array
; = 2: $A is empty
; = 3: first element isn't a array
; Author ........: AspirinJunkie
; =================================================================================================
Func _ArrayAinATo2d(ByRef $A)
If UBound($A, 0) <> 1 Then Return SetError(1, UBound($A, 0), Null)
Local $N = UBound($A)
If $N < 1 Then Return SetError(2, $N, Null)
Local $u = UBound($A[0])
If $u < 1 Then Return SetError(3, $u, Null)
Local $a_Ret[$N][$u]
For $i = 0 To $N - 1
Local $t = $A[$i]
If UBound($t) > $u Then ReDim $a_Ret[$N][UBound($t)]
For $j = 0 To UBound($t) - 1
$a_Ret[$i][$j] = $t[$j]
Next
Next
Return SetExtended($N, $a_Ret)
EndFunc ;==>_ArrayAinATo2d
; #FUNCTION# ======================================================================================
; Name ..........: _ArrayJoin()
; Description ...: Combines 2 2D arrays via corresponding properties (actual data or user-defined calculated) similar to JOIN in relational databases
; Syntax ........: _ArrayJoin($aA, $aB, [$vCompA = 0, [$vCompB = Default, [$sJoinType = "inner"]]])
; Parameters ....: $aA - 2D array or Array-In-Array which should joined with $aB
; $aB - 2D array or Array-In-Array which should joined with $aA
; $vCompA - Rule for determining the link key for $aA.
; defaults to 0 = value of first column element
; Can be:
; | single Integer: Column-Index for direct values
; | Integer-Array: combined multiple direct values
; | user-defined function: calculation rule as a function of the form
; function($a1DArray, $dummy): get the current array row as 1D-Array and calculate the key
; | String: AutoIt-Code as string to calculate the key
; Must contain "$A", where "$A" represents the current array line as a 1D array.
; $vCompB - the same like $vCompA but for Array $aB
; defaults to the same value as $aA
; $sJoinType - type of joining (see https://www.w3schools.com/sql/sql_join.asp for explanation)
; on of these:
; | "inner" (default): inner join - Returns records that have matching values in both tables
; | "left" : left (outer) join - Returns all records from the left table, and the matched records from the right table
; | "right": right (outer) join - Returns all records from the right table, and the matched records from the left table
; | "outer" or "full": (full) outer join - Returns all records when there is a match in either left or right table
; Return values .: Success: combined values as 2D-Array with columns of $aA first and $aB following. @extended = number of rows
; Failure: null and set error to:
; | @error = 1 : $aA is not a 1D/2D array
; | @error = 2 : $aB is not a 1D/2D array
; | @error = 3 : No data in $aA
; | @error = 4 : No data in $aB
; | @error = 5 : $aA is not a valid array-in-array
; | @error = 6 : $aB is not a valid array-in-array
; | @error = 7 : no valid form for $vCompA
; | @error = 8 : no valid form for $vCompB
; | @error = 9 : $vCompA is a string but it does not contain $A
; | @error = 10: $vCompB is a string but it does not contain $A
; | @error = 11: An array was passed for $vCompA but not a 1D array - invalid
; | @error = 12: An array was passed for $vCompB but not a 1D array - invalid
; | @error = 13: no valid value for $sJoinType passed
; | @error = 14: No joins found - return array is therefore empty
; Author ........: aspirinjunkie
; Modified ......: 2024-02-12
; Related .......: __ap_cb_getKey_Index_Single(), _Array__ap_cb_getKey_String(), __ap_cb_getKey_Index_Multi(), _Array2dToAinA()
; Example .......: Yes
; Global $aProds[][3] = [["apple", 1.5, "fruits"], ["Pancake", 3.0, "sweets"], ["banana", 2.0, "fruits"], ["banana", 1.0, "plastic"]]
; Global $aColors[][2] = [["apple", "red"], ["coconut", "brown"], ["banana", "yellow"], ["banana", "brown"]]
;
; ; full outer join by first value of both arrays
; $aJoin = _ArrayJoin($aProds, $aColors, 0, 0, "outer")
; _ArrayDisplay($aJoin, "by first value")
;
; ; join using the first letter of first value in both arrays
; $aJoin = _ArrayJoin($aProds, $aColors, "StringLeft($A[0], 1)")
; _ArrayDisplay($aJoin, "by first letter")
; =================================================================================================
Func _ArrayJoin($aA, $aB, $vCompA = 0, $vCompB = Default, $sJoinType = "inner")
Local $bCbIsString = False
; same key descriptor for both arrays (if $vCompB = Default)
If IsKeyword($vCompB) = 1 Then $vCompB = $vCompA
; variables which describe the both Arrays
Local $nDimsA = UBound($aA, 0), $nDimsB = UBound($aB, 0), _
$nRowsA = UBound($aA, 1), $nRowsB = UBound($aB, 1), _
$nColsA = UBound($aA, 2), $nColsB = UBound($aB, 2)
If $nRowsA < 1 Then Return SetError(3, $nRowsA, Null)
If $nRowsB < 1 Then Return SetError(4, $nRowsB, Null)
; prepare Array A (convert into Array-In-Array)
Switch $nDimsA
Case 1 ; already Array-In-Array
If Not IsArray($nRowsA[0]) Then Return SetError(5, 0, Null)
Case 2 ; 2D-Array in Array-In-Array
; already dimension the number of sub-elements for the result array
ReDim $aA[$nRowsA][$nColsA + $nColsB]
; convert into Array-In-Array for better handling in the next steps
$aA = _Array2dToAinA($aA)
Case Else
Return SetError(1, $nDimsA, Null)
EndSwitch
; prepare Array B (convert into Array-In-Array)
Switch $nDimsB
Case 1 ; already Array-In-Array
If Not IsArray($nRowsB[0]) Then Return SetError(6, 0, Null)
Case 2
; convert into Array-In-Array for better handling in the next steps
$aB = _Array2dToAinA($aB)
Case Else
Return SetError(2, $nDimsB, Null)
EndSwitch
; prepare the key extraction function for $aA
Local $cbKeyA
Select
Case IsInt($vCompA) ; single array index as key
$cbKeyA = __ap_cb_getKey_Index_Single
Case IsFunc($vCompA) ; user defined function
$cbKeyA = $vCompA
Case IsString($vCompA) ; function directly as a string
If Not StringInStr($vCompA, '$A', 2) Then Return SetError(9, 0, Null)
Local $bBefore = Opt("ExpandEnvStrings", 1)
$cbKeyA = __ap_cb_getKey_String
$bCbIsString = True
Case IsArray($vCompA) ; multiple indices
If UBound($vCompA, 0) <> 1 Then Return SetError(11, UBound($vCompA, 0), Null)
$cbKeyA = __ap_cb_getKey_Index_Multi
Case Else ; no valid form for $vCompA
Return SetError(7, 0, Null)
EndSelect
; prepare the key extraction function for $aB
Local $cbKeyB
Select
Case IsInt($vCompB) ; single array index as key
$cbKeyB = __ap_cb_getKey_Index_Single
Case IsFunc($vCompB) ; user defined function
$cbKeyB = $vCompB
Case IsString($vCompB) ; function directly as a string
If Not StringInStr($vCompB, '$A', 2) Then Return SetError(10, 0, Null)
Local $bBefore = Opt("ExpandEnvStrings", 1)
$cbKeyB = __ap_cb_getKey_String
$bCbIsString = True
Case IsArray($vCompB) ; multiple indices
If UBound($vCompB, 0) <> 1 Then Return SetError(12, UBound($vCompB, 0), Null)
$cbKeyB = __ap_cb_getKey_Index_Multi
Case Else ; no valid form for $vCompB
Return SetError(8, 0, Null)
EndSelect
; convert $aA into Map
Local $mA[], $aData, $sKey
For $i = 0 To $nRowsA - 1
$aData = $aA[$i]
$sKey = $cbKeyA($aData, $vCompA)
Local $aSubElements[1]
If MapExists($mA, $sKey) Then ; record with same key already exists
$aSubElements = $mA[$sKey]
ReDim $aSubElements[UBound($aSubElements) + 1]
EndIf
$aSubElements[UBound($aSubElements) - 1] = $aData
$mA[$sKey] = $aSubElements
Next
; convert $aB into Map
Local $mB[]
For $i = 0 To $nRowsB - 1
$aData = $aB[$i]
$sKey = $cbKeyB($aData, $vCompB)
Local $aSubElements[1]
If MapExists($mB, $sKey) Then ; record with same key already exists
$aSubElements = $mB[$sKey]
ReDim $aSubElements[UBound($aSubElements) + 1]
EndIf