forked from DonovanChan/fmfunctions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayColumnLoop.calc
51 lines (42 loc) · 1.94 KB
/
ArrayColumnLoop.calc
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
Let ( [
rowDelimiter = If ( IsEmpty ( rowDelimiter ) ; "¶" ; rowDelimiter ) ;
rowDelimiterLen = Length ( rowDelimiter ) ;
arrayRows = rowDelimiter & array & rowDelimiter ;
rowStart = Position ( arrayRows ; rowDelimiter ; 1 ; 1 ) + rowDelimiterLen ;
rowEnd = Position ( arrayRows ; rowDelimiter ; rowStart + 1 ; 1 ) - rowDelimiterLen ;
row = Middle ( arrayRows ; rowStart ; rowEnd - rowStart + rowDelimiterLen ) ;
// Substituting the column delimiter isn't the fastest, but it will do for now
cell = GetValue ( Substitute ( row ; columnDelimiter ; ¶ ) ; columnIndex ) ;
rowsRemaining = Middle ( array ; rowEnd + rowDelimiterLen ; Length ( array ) - rowEnd ) ;
result = If ( not IsEmpty ( previousResults ) ; previousResults & ¶ ) & cell
] ;
Case (
not IsEmpty ( rowsRemaining ) ;
ArrayColumnLoop ( rowsRemaining ; columnIndex ; columnDelimiter ; rowDelimiter ; result ) ;
result
)
)
/* __________________________________________________
NAME: ArrayColumnLoop ( array ; columnIndex ; columnDelimiter ; rowDelimiter ; previousResults )
PURPOSE: Performs recursion for ArrayColumn()
TESTS:
List (
ArrayColumnLoop ( "City|State|Country" ; 2 ; "|" ; "" ; "" )
= "State" ;
ArrayColumnLoop ( "City||Country¶Oakland||US" ; 2 ; "|" ; "" ; "" )
= "" ;
ArrayColumnLoop ( "City||State||Country+++||||+++Portland||OR||US" ; 1 ; "||" ; "+++" ; "" )
= "City¶¶Portland" ;
ArrayColumnLoop ( "City||State||Country+++||||+++Portland||OR||US" ; 2 ; "||" ; "+++" ; "" )
= "State¶¶OR" ;
ArrayColumnLoop ( "City||State||Country+++||||+++Portland||OR||US" ; 3 ; "||" ; "+++" ; "" )
= "Country¶¶US" ;
ArrayColumnLoop ( "City||State||Country" ; 1 ; "||" ; "" ; "" )
= "City" ;
ArrayColumnLoop ( "City|State|Country¶Oakland|CA|US¶Portland|OR|US" ; 2 ; "" ; "¶" ; "" )
= ""
)
HISTORY:
Created: 2011-10-24 19h43 PST — Donovan Chandler
Modified: 2014-03-20 11:46 PDT - Donovan Chandler : Fixed bug where last value was omitted; Added rowDelimiter param.
*/