I've been using the following scripts (created by Chris Burke) for years
only to realize that they are not part of the standard library:
1. cutstring - boxes string using a delimiter but respects quotes
2. delimitlist - creates a string from a boxed list delimited by a
string
3. commasep - creates a string from a boxed list separated by comma
4. datasep - same as commasep but each boxed item is enclosed by quotes
5. charsep - separates a boxed item by a character (suspiciously similar
to delimitlist)
I was wondering if it would be possible to add this as part of a
standard library or as an add-on (I would prefer standard library). How
do I go about getting it in?
Hmmm. There is a possibility that there are existing methods in the
library/addons that does the following. If so, I would be happy to
switch to using that.
Here are the codes:
NB. =========================================================
NB.*cutstring v cut string into boxes
NB.
NB. examples:
NB.
NB. cutstring ' one, two,,three'
NB. +----+-----++-----+
NB. | one| two||three|
NB. +----+-----++-----+
NB.
NB. cutstring ' one, ''two,three'',,four'
NB. +----+-----------++----+
NB. | one| two,three||four|
NB. +----+-----------++----+
NB.
NB. cutstring ' ,one, ''two, ''''three'''',four'',,''Ron''''s
Stuff'','
NB. +-+---+---------------------++-----------++
NB. | |one| two, 'three',four||Ron's Stuff||
NB. +-+---+---------------------++-----------++
NB.
NB. cutstring ',,''one, two'',,three'
NB. +++---------++-----+
NB. |||one, two||three|
NB. +++---------++-----+
NB.
NB. '2' cutstring 'The2flux2capacitor2requires21.32jigawatt'
NB. +---+----+---------+--------+---+--------+
NB. |The|flux|capacitor|requires|1.3|jigawatt|
NB. +---+----+---------+--------+---+--------+
cutstring=: ','&$: : (4 : 0)
if. 0 = #y do. '' return. end.
if. L. y do. y return. end.
txt=. y, {.x
msk=. txt = ''''
com=. (txt e. x) > ~: /\ msk
msk=. (msk *. ~:/\msk) < msk <: 1 |. msk
NB. Modified so that it won't delete the extra blanks
NB. deb each (msk # com) <;._2 msk # txt
(msk # com) <;._2 msk # txt
)
NB. =========================================================
NB.*delimitlist v return list separated by delimiters
delimitlist=: 4 : 0
dat=. y
if. L. dat do.
}. ; (x&, @ ":) each dat
else.
dat=. ": dat
dat=. deb ' ' (bx dat=LF) } dat
x (bx dat=' ') } dat
end.
)
NB. =========================================================
NB.*commasep v Separates a boxed item with comma
NB.
NB. see also datasep
commasep=: 3 : 0
if. L. y do.
}. ; (','&, @ ":) each y
else.
, ": y
end.
)
NB. =========================================================
NB.*datasep v Separates a boxed item with comma but enclosed with quotes
NB.
NB. see also commasep
datasep=: 3 : 0
if. L. y do.
}. ; ','''&,@(,&'''') @ (#~ >:@(=&'''')) @ ": each y
else.
y=. ": y
'''', ((>: y = '''') # y), ''''
end.
)
NB. =========================================================
NB.*charsep v Separates a boxed item with a character
NB.
NB. see also commasep
charsep=: 3 : 0
',' charsep y
:
if. L. y do.
}. ; (x &, @ ":) each y
else.
, ": y
end.
)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm