overkill and not polished yet, but
Y =: (&{::)(@:])
X =: (&{::)(@:[)
M=:@:]
T=:(&{)(>@)
by2 =: 1 :' 2 u\ ]'
R =: (&}.) M
Dil =: ;@:((0 X {. ]) (,~ <)~ 2 R~ (, <) (0 X + 1 X) }. ])"1 1 NB. idx len
item list:X str :Y item optional
Mil =: <@((0 X (+ i.) 1 X) { ])"1 1 NB. len idx item list:X str :Y
Eil =: ((#M~ ;~"0 0 I.@E.) ) NB. returns len idx of matches
EilR =: (]Dil~"1 1 (1 R)~,~("1) 0 X Eil ]) NB. replace based on Eli. 2items :X
Eli/E. arg:0X list compatible with y :1X
EilRI =: (] Dil~"1 1 ([: 1 R 0 X) ,~("1 1) 1 X { (0; 0) X Eil]) :: ] NB.
replace select indexes of Eli. 2 items tree :x EilR args :0X arg to { (0
first, _1 last etc) :1X. index error makes no replacement.
numbers =: 0 ". (#~ e.&'0123456789')
pD =: 1!:2&2reduce =: 1 : '<"_1@[ ([: u (&.>)/(>@:) ,) <@:]'numerify =:
0&".^:(2 = 3!:0)
maybenum =: 0&".^:(] -:&linearize ":@:numerify)
linearize =: (, $~ 1 -.~ $)
multicut =: [:>[:cut leaf/ ([: <"_1 ({&a.)^:(1 4 e.~
3!:0)@:linearize@:maybenum@:[) (, <) ]
booldots =: '[0123456789]+\:([\:\.]+)'&(':.' >@(#[email protected]) {:@:{:@:rxmatches
Mil ])
tplrplcU =: '`'&cut M~ (] ,"1 0 booldots leaf@(1 Y)"1 ([ { >:@(>./)M~ $ ]) [)
'[0123456789]+\:[\:\.]+' (,/@:rxmatches ([ ;"1 1 (] , numbers leaf)@Mil) ]) ]
tplrplcgetN =: ('([0123456789]+)\:[\:\.]+' ({:"2@rxmatches ([ ;"1 1 (] ,
numbers leaf)@Mil) ]) ])
tplrplcgetS =: ( '([[:alpha:]]+)\:[\:\.]+'&((] (, <)"1 0 {:"1 i.~
~.@:({:"1))@:({:"2@rxmatches ([ ;"1 0 Mil) ])) )
tplrplcgetM =: tplrplcgetN (/:~)@([ , 0 1&{"1 M (,<)"1 0 (2 {::"1 ]) { (2 {::"1
[) -.~ i.@+&#&:(2 Y"1)) tplrplcgetS
tplrplcSN =: ('([[:alpha:]]+)\:[\:\.]+'&(('`' joinstring ] , each ' ' <@,"1
":@:,.@:i.@#)@:({:"2@rxmatches Mil ])) $: ]) : (] Dil reduce~ ' `'&multicut M~
( ;/@(0 Y)"1 ,"1 0 (1 {:: each [) ":each@:{~ (0 {:: each [) i."_ 0 {:"1 M)
'([[:alpha:]]+)\:[\:\.]+' ({:"2@rxmatches ([ ;"1 0 Mil) ]) ])
tplrplcS =: (] Dil reduce~ '`'&multicut M~ (;/@(0 Y"1) ,"1 0 [ {~ 2
Y"1)'([[:alpha:]]+\:[\:\.]+)' (] ,"1 0 <"0@(~. i. ])@:(1 Y"1))@:({:"2@rxmatches
([ ;"1 0 Mil) ]) ])
TPLN =: 2 : 0 NB.daFx
for_i. |. a =. n tplrplcU m do. m =. m Dil~ (;/@(0 Y) (, <) 3 Y) i end.
(pD m) sprintf (2 Y"1 a)&{ M
)
Most of this code is not needed, but I have a version that uses text as
template items as well, but uses more library code,
'hello 0::1 0::2 1:. ' TPLN '%j`%s'
'hello %j1 %j2 %s ' sprintf 0 0 1&{@:]
'hello 0::1 0::2 1:. ' TPLN '%j`%s' 0;'there'
hello 01 02 there
the templating for TPLN uses numbers only. Its dual templating. A number
(other version can be text) followed by one : and a string of : or .'s that are
a binary code
:: = 0
:. = 1
:.: = 2
:.. = 3
::: = 0
::. = 1
these codes are indexes into the n argument to TPLN. The numeric index is an
index into y.
The point is that you may reuse multiple indexes into y, but format them
differently each time. (below assumes lr_z_ is defined). Format codes that
include trailing . or : are necessary to add the punctuation. Can't use `
'hello 0::1 0::2 1:. 2:.:' TPLN '%j`%?lr_z_?j`%j.' 0;'there';'Joe'
hello 01 02 'there' Joe.
----- Original Message -----
From: Joe Bogner <[email protected]>
To: [email protected]
Sent: Wednesday, January 6, 2016 9:37 PM
Subject: [Jprogramming] verb to template a string
I often find myself joining strings with x/y
The most recent example was:
('http://real-chart.finance.yahoo.com/table.csv?s=' ,
]@,&'&a=10&b=2&c=1993&d=00&e=6&f=2016&g=d&ignore=.csv') 'TICKER'
I think it would be handy to have a simple verb that can take a box of
strings / indices and merge it with a box of inputs
Something like:
('hello ';0;0;0) applyTemplate ('a';'b')
Would result in
hello aaa
('hello ';0;' hello';1) applyTemplate ('a';'b')
would be:
hello a hello b
I struggled with this for about 25 minutes to make it tacit and gave
up. Any takers?
(I thought about using printf but figured it was too heavy)
applyTemplate =: 4 : 0
str=.''
for_p. x do.
str=.str,(( y {::~ ]) :: ] ) (>p)
end.
)
('hello ';0;' hello ';1) applyTemplate ('a';'b')
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm