ASCAN

ASCAN(a,[d])
a
array
[d]
direction argument ; 0 or omitted scan by array, -1 by rows, 1 by clms

Array SCAN, 3 in 1 function, SCAN by row, by column, by array

Xlambda

Well-known Member
Joined
Mar 8, 2021
Messages
832
Office Version
  1. 365
Platform
  1. Windows
ASCAN, Array SCAN, 3 in 1 function, SCAN by row, by column, by array. This is my take of tboulden's SCANBYROW/BYCOL.
Uses only new!! SCAN lambda helper function, no need of byrow,bycol,makearray, or lambda as arguments.
Excel Formula:
=LAMBDA(a,[d],
    LET(y,IF(d=1,TRANSPOSE(a),a),s,SCAN(0,y,LAMBDA(v,a,v+a)),r,ROWS(s),c,COLUMNS(s),sr,SEQUENCE(r)-1,
       x,s-IF(d,IFERROR(INDEX(INDEX(s,,c),sr)*sr^0,0),0),
       IF(d=1,TRANSPOSE(x),x)
   )
)
LAMBDA 1.1.3.xlsx
ABCDEFGHIJKLMNOPQRST
1d,omitted (by array)d,-1 (by rows)d,1 (by clms)
2sample 1a=ASCAN(A3:D6)=ASCAN(A3:D6,-1)=ASCAN(A3:D6,1)
3123413610136101234
45678152128365111826681012
5910111245556678919304215182124
613141516911051201361327425828323640
7
8d,omitted (by array)d,-1 (by rows)d,1 (by clms)
9sample 2a=ASCAN(A10#)=ASCAN(A10#,-1)=ASCAN(A10#,1)
10123413610136101234
115678152128365111826681012
12910111245556678919304215182124
1313141516911051201361327425828323640
14171819201531711902101735547445505560
15212223242312532763002143669066727884
16
17Note: Did not set an extra argument for any kind of "initial value", since the only thing it does is too simple,
18adds a constant value to the final outcome , like in 10+ASCAN(a)
19
ASCAN post
Cell Formulas
RangeFormula
F2,P9,K9,F9,P2,K2F2=FORMULATEXT(F3)
F3:I6F3=ASCAN(A3:D6)
K3:N6K3=ASCAN(A3:D6,-1)
P3:S6P3=ASCAN(A3:D6,1)
A10:D15A10=SEQUENCE(6,4)
F10:I15F10=ASCAN(A10#)
K10:N15K10=ASCAN(A10#,-1)
P10:S15P10=ASCAN(A10#,1)
Dynamic array formulas.
 
Upvote 0
BYROW still cannot "spill" rows.
Correct, but as I mentioned in SCANBYROW, we can thunk the results and get them back out with MAKEARRAY; I think the usefulness of this to have an array-of-arrays will be important for some use-cases.

Cell Formulas
RangeFormula
I5:L7I5=LET( thunks, BYROW(A1:G3, LAMBDA(row_, LAMBDA(TRANSPOSE(UNIQUE(TRANSPOSE(row_)))) )), rows_,ROWS(thunks), cols_,MAX(MAP(thunks,LAMBDA(thunk,COLUMNS(thunk())))), MAKEARRAY(rows_,cols_, LAMBDA(i,j, INDEX( INDEX(thunks,i,1)(), 1, j ) ) ) )
N5N5=FORMULATEXT(I5)
Dynamic array formulas.
 
Of course it can be tricked in even more ways, but still, not for real life. One day excel will make it dynamic.? ✌
 
SCAN vs BYROW
LAMBDA 1.1.4.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXY
1SCAN vs BYROW versatility
2In general SCAN is more versatile because we can control the "iteration argument".
3Also we are not limited to one direction, we can use sections of an array, plus initial value could be "silent" or cumulative.
4Examples:
51. First time seen on one of MrExcel YT videos , an unusual type of sequencial extraction.
6Instead of a regular:he needed this :For SCAN versatility, very easy
7=SCAN("",SEQUENCE(6),LAMBDA(v,i,LET(j,IF(MOD(i,2),1,-1),TEXTJOIN("|",,SORT(INDEX(A8#,i,),,j,1)))))
812345123451|2|3|4|5
967891010987610|9|8|7|6=ATEXTSPLIT(M8#,"|")
101112131415111213141511|12|13|14|1512345
111617181920201918171620|19|18|17|16109876
122122232425212223242521|22|23|24|251112131415
132627282930302928272630|29|28|27|262019181716
14("snake" sequence)2122232425
153029282726
16MrExcel YT link
17Better ESPN Fantasy Draft Recap In Excel Free Download - 2426
18
192. dynamic selection
20
21sort ascending between rows 2
226
23
24sample=ATEXTSPLIT(SCAN("",SEQUENCE(G22-G21+1,,G21),LAMBDA(v,i,TEXTJOIN("|",,SORT(INDEX(A25:D31,i,),,,1)))),"|")
2565721245
2642155689
2786593678
2873861459
29915425710
3010572
3158107
32
ASCAN post 8
Cell Formulas
RangeFormula
M7,G24,O9M7=FORMULATEXT(M8)
A8:E13A8=SEQUENCE(6,5)
M8:M13M8=SCAN("",SEQUENCE(6),LAMBDA(v,i,LET(j,IF(MOD(i,2),1,-1),TEXTJOIN("|",,SORT(INDEX(A8#,i,),,j,1)))))
O10:S15O10=ATEXTSPLIT(M8#,"|")
G25:J29G25=ATEXTSPLIT(SCAN("",SEQUENCE(G22-G21+1,,G21),LAMBDA(v,i,TEXTJOIN("|",,SORT(INDEX(A25:D31,i,),,,1)))),"|")
Dynamic array formulas.
 
TEXTJOIN vs ARRAYTOTEXT
LAMBDA 1.1.4.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACAD
1TEXTJOIN vs ARRAYTOTEXT
2TEXTJOIN is more versatile in general because of its "ignore empty" argument, and its flexibility to choose any delimiter, even array delimiter.
3ARRAYTOTEXT, only "," delimiter, no "ignore empty". "," could be a problem with the final splitting, if the cells contain already ","'s.
4With textjoin, no problem, we choose any delim we want.
5
61. Extract only the unique numeric values of each row of the following array (ae)
7
8unique all values
9ae=SCAN("",SEQUENCE(3),LAMBDA(v,i,TEXTJOIN("|",1,UNIQUE(INDEX(ae,i,),1))))
10b33aa4d14b|3|a|4|d|1
117xx5x5y8x7|x|5|y|8
12m6nn66mo6m|6|n|o
13
14textthe only thing we have to modify for numeric values
15numbersis "ae" with:=IF(ISTEXT(ae),"",ae), textjoin will ignore the gaps=ARRAYTOTEXT(L16:T16) does not
16 33414, 3, 3, , , 4, , 1, 4
177558=ARRAYTOTEXT(L16:T16,1) still not good
186666{"",3,3,"","",4,"",1,4}
19
20=SCAN("",SEQUENCE(3),LAMBDA(v,i,TEXTJOIN("|",1,UNIQUE(INDEX(IF(ISTEXT(ae),"",ae),i,),1))))
21=ATEXTSPLIT(K22#,"|")
223|4|1341
237|5|8758other functions
2466ATEXTSPLIT
25
261. Extract only the unique non numeric values of each row of (ae)
27
28single cell ( we use ae=IF(ISNUMBER(ae),"",ae)
29
30=ATEXTSPLIT(SCAN("",SEQUENCE(3),LAMBDA(v,i,TEXTJOIN("|",1,UNIQUE(INDEX(IF(ISNUMBER(ae),"",ae),i,),1)))),"|")
31bad
32xyNotes:
33mnoNo need to say that whatever crazy conditions will apply to initial array, the formula will comply.
34If we need, for example, unique extraction of numeric values >=10 and <100, no problem.
35Rhetoric question: What custom-made function can ever cover everything that a 5" long lambda "formula" can do in 2 sec?
36Very few lambda functions will survive.
37
ASCAN post 9
Cell Formulas
RangeFormula
K9,D30,N21,L15K9=FORMULATEXT(K10)
K10:K12K10=SCAN("",SEQUENCE(3),LAMBDA(v,i,TEXTJOIN("|",1,UNIQUE(INDEX(ae,i,),1))))
Z15Z15=FORMULATEXT(Z16)&" does not"
L16:T18L16=IF(ISTEXT(ae),"",ae)
Z16Z16=ARRAYTOTEXT(L16:T16)
Z17Z17=FORMULATEXT(Z18)&" still not good"
Z18Z18=ARRAYTOTEXT(L16:T16,1)
K20K20=FORMULATEXT(K22)
K22:K24K22=SCAN("",SEQUENCE(3),LAMBDA(v,i,TEXTJOIN("|",1,UNIQUE(INDEX(IF(ISTEXT(ae),"",ae),i,),1))))
N22:P24N22=ATEXTSPLIT(K22#,"|")
D31:F33D31=ATEXTSPLIT(SCAN("",SEQUENCE(3),LAMBDA(v,i,TEXTJOIN("|",1,UNIQUE(INDEX(IF(ISNUMBER(ae),"",ae),i,),1)))),"|")
Dynamic array formulas.
Named Ranges
NameRefers ToCells
ae='ASCAN post 9'!$A$10:$I$12L16, K22, K10, D31
 
Below I perform practically the same snake-sequence calc with BYROW/TEXTJOIN that you're performing with SCAN/TEXTJOIN. The main difference is BYROW construction has SEQUENCE(,5) in the TEXTJOIN, not sure why your version doesn't also work with BYROW, any ideas?

The whole point of the LAMBDA helpers is to add more tools, so there will be multiple ways to do the same task generally speaking; I'm mainly concerned about clarity and best practices. In your SCAN construction, the init_value can be anything at all because it is ignored since the v param in your LAMBDA is never used.

The TEXTJOIN vs ARRAYTOTEXT points are good to note, but I skip needing either one by thunking the arrays and using MAKEARRAY and without needing ATEXTSPLIT.

Personally, I'd rather do snake-sequence calc using MAP/thunks/MAKEARRAY as below than BYROW/BYCOL.

Cell Formulas
RangeFormula
A1:E6A1=SEQUENCE(6,5)
G1:G6G1=SCAN( "", SEQUENCE(6), LAMBDA(v,i, LET( j,IF(MOD(i,2),1,-1), TEXTJOIN("|",,SORT(INDEX(A1#,i,),,j,1)) )) )
L1,L20,L11L1=FORMULATEXT(G1)
G11:G16G11=BYROW( SEQUENCE(6), LAMBDA(i, LET( j,IF(MOD(i,2),1,-1), TEXTJOIN("|",,SORT(INDEX(A1#,i,SEQUENCE(,5)),,j,1)) )) )
G20:K25G20=LET( array,TRANSPOSE(A1#),rows_,ROWS(array), cols_,COLUMNS(array), seq,SEQUENCE(,cols_),sort_params,IF(ISEVEN(seq),-1,1), thunks, MAP(seq,sort_params, LAMBDA(idx,order, LAMBDA(SORT(INDEX(array,,idx),,order)) )), expand, MAKEARRAY(rows_,cols_, LAMBDA(i,j, INDEX(INDEX(thunks,1,j)(),i,1) )), TRANSPOSE(expand) )
Dynamic array formulas.
 
?if you noticed, my threads have, in general, more than one post. Sometimes they reveal more ways of solving same problem, sometimes they are based on a concept, they have a foundation and a structure that builds up in a flow of examples, from basic simple ones to more complex ones at the end. So, concept, foundation, flow, all consistent with each other. The concept here is: lambda "formula" (mandatory to be short), foundation is SCAN/REDUCE, flow....
The flow just started, and you came in with alternatives. Very nice! I am open to all alternatives, but here the "foundation" was SCAN as the thread name states (consistency again), pushing it to the limits. I will finish the flow other time, no problem.
Anyhow, single lambda like SCAN versus 2 other lambdas whatever they are BYROW and MAKEARRAY or MAP and other…come on, never. I did combinatorics with all possible formulas and methods, when we deal with 10M cells a simple comma added makes the difference between 5s and eternity.
Regarding ATEXTSPLIT; Why we use any function? Why we use UNIQUE? We can still do it old school. The answer is quite simple. We use them because we have them, and they do their job. As simple as that.
To conclude on a funny note. You know, obviously, what cab sharing does, more popular in US. I was in a cab heading to SCAN, you jump in asking me if we can detour to BYROW , normally is no problem, if I am not in a hurry.?✌
 
Snake sequence RxC . An example of what fits my personal "concept" of a lambda "formula" in general.
=MAKEARRAY(R,C,LAMBDA(r,c,C*(r-1)+c*ISODD(r)+(C-c+1)*ISEVEN(r)))
LAMBDA 1.1.4.xlsx
ABCDEFGHIJK
1rows7
2clms8
3
4=MAKEARRAY(B1,B2,LAMBDA(r,c,B2*(r-1)+c*ISODD(r)+(B2-c+1)*ISEVEN(r)))
512345678
6161514131211109
71718192021222324
83231302928272625
93334353637383940
104847464544434241
114950515253545556
12
Sheet11
Cell Formulas
RangeFormula
A4A4=FORMULATEXT(A5)
A5:H11A5=MAKEARRAY(B1,B2,LAMBDA(r,c,B2*(r-1)+c*ISODD(r)+(B2-c+1)*ISEVEN(r)))
Dynamic array formulas.
 
To conclude on a funny note. You know, obviously, what cab sharing does, more popular in US. I was in a cab heading to SCAN, you jump in asking me if we can detour to BYROW , normally is no problem, if I am not in a hurry.?✌
This is my take of tboulden's SCANBYROW/BYCOL.
Not sure the car-sharing metaphor holds up since I inspired the thread :)
 
?You can own the cab company, still is cab sharing?
 
Back on track with more examples:
LAMBDA 1.1.4.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAI
1Task: "scan" whole array by each product
2
3sample array 6 rows, 3, products unique prod."scan" whole array, not what we want"scan" by row, not what we want
4prod.=UNIQUE(A5:A10)=SCAN(0,B5:F10,LAMBDA(v,a,v+a))or=ASCAN(B5:F10)=ASCAN(B5:F10,-1)
5A86863A814222831814222831814222831
6B79875B38475562673847556267716243136
7A49923C71808991947180899194413222427
8C14151959910010510695991001051061561112
9B6327811211511712413211211511712413269111826
10C56881137143151159160137143151159160511192728
11
12scan only for prod=Ascan only prod=Bscan only prod=Csum all scans
13=J14#+P14#+V14#
148142228310000000000814222831
150000071624313600000716243136
16354453555800000000003544535558
17000000000015611121561112
18000004245475462000004245475462
19000000000017233139401723313940
20
21K14:=SCAN(0,(B5:F10)*(A5:A10=INDEX(H5#,1)),LAMBDA(v,a,v+a))*((A5:A10=INDEX(H5#,1)))
22Q14:=SCAN(0,(B5:F10)*(A5:A10=INDEX(H5#,2)),LAMBDA(v,a,v+a))*((A5:A10=INDEX(H5#,2)))
23W14:=SCAN(0,(B5:F10)*(A5:A10=INDEX(H5#,3)),LAMBDA(v,a,v+a))*((A5:A10=INDEX(H5#,3)))
24
25single cell
26=REDUCE(0,SEQUENCE(3),LAMBDA(v,i,v+SCAN(0,(B5:F10)*(A5:A10=INDEX(H5#,i)),LAMBDA(v,a,v+a))*((A5:A10=INDEX(H5#,i)))))
27814222831
28716243136
293544535558
301561112
314245475462
321723313940
33
ASCAN post 10
Cell Formulas
RangeFormula
H4,L4,J26,AB13,AA4,U4H4=FORMULATEXT(H5)
H5:H7H5=UNIQUE(A5:A10)
L5:P10L5=SCAN(0,B5:F10,LAMBDA(v,a,v+a))
U5:Y10U5=ASCAN(B5:F10)
AA5:AE10AA5=ASCAN(B5:F10,-1)
J14:N19J14=SCAN(0,(B5:F10)*(A5:A10=INDEX(H5#,1)),LAMBDA(v,a,v+a))*((A5:A10=INDEX(H5#,1)))
P14:T19P14=SCAN(0,(B5:F10)*(A5:A10=INDEX(H5#,2)),LAMBDA(v,a,v+a))*((A5:A10=INDEX(H5#,2)))
V14:Z19V14=SCAN(0,(B5:F10)*(A5:A10=INDEX(H5#,3)),LAMBDA(v,a,v+a))*((A5:A10=INDEX(H5#,3)))
AB14:AF19AB14=J14#+P14#+V14#
J21J21=FORMULATEXT(J14)
J22J22=FORMULATEXT(P14)
J23J23=FORMULATEXT(V14)
J27:N32J27=REDUCE(0,SEQUENCE(3),LAMBDA(v,i,v+SCAN(0,(B5:F10)*(A5:A10=INDEX(H5#,i)),LAMBDA(v,a,v+a))*((A5:A10=INDEX(H5#,i)))))
Dynamic array formulas.
 

Forum statistics

Threads
1,214,869
Messages
6,122,012
Members
449,060
Latest member
LinusJE

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top