2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
53
Task/Multisplit/Lua/multisplit.lua
Normal file
53
Task/Multisplit/Lua/multisplit.lua
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
--[[
|
||||
Returns a table of substrings by splitting the given string on
|
||||
occurrences of the given character delimiters, which may be specified
|
||||
as a single- or multi-character string or a table of such strings.
|
||||
If chars is omitted, it defaults to the set of all space characters,
|
||||
and keep is taken to be false. The limit and keep arguments are
|
||||
optional: they are a maximum size for the result and a flag
|
||||
determining whether empty fields should be kept in the result.
|
||||
]]
|
||||
function split (str, chars, limit, keep)
|
||||
local limit, splitTable, entry, pos, match = limit or 0, {}, "", 1
|
||||
if keep == nil then keep = true end
|
||||
if not chars then
|
||||
for e in string.gmatch(str, "%S+") do
|
||||
table.insert(splitTable, e)
|
||||
end
|
||||
return splitTable
|
||||
end
|
||||
while pos <= str:len() do
|
||||
match = nil
|
||||
if type(chars) == "table" then
|
||||
for _, delim in pairs(chars) do
|
||||
if str:sub(pos, pos + delim:len() - 1) == delim then
|
||||
match = string.len(delim) - 1
|
||||
break
|
||||
end
|
||||
end
|
||||
elseif str:sub(pos, pos + chars:len() - 1) == chars then
|
||||
match = string.len(chars) - 1
|
||||
end
|
||||
if match then
|
||||
if not (keep == false and entry == "") then
|
||||
table.insert(splitTable, entry)
|
||||
if #splitTable == limit then return splitTable end
|
||||
entry = ""
|
||||
end
|
||||
else
|
||||
entry = entry .. str:sub(pos, pos)
|
||||
end
|
||||
pos = pos + 1 + (match or 0)
|
||||
end
|
||||
if entry ~= "" then table.insert(splitTable, entry) end
|
||||
return splitTable
|
||||
end
|
||||
|
||||
local multisplit = split("a!===b=!=c", {"==", "!=", "="})
|
||||
|
||||
-- Returned result is a table (key/value pairs) - display all entries
|
||||
print("Key\tValue")
|
||||
print("---\t-----")
|
||||
for k, v in pairs(multisplit) do
|
||||
print(k, v)
|
||||
end
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
sub multisplit($str, @seps) { $str.split(/ ||@seps /, :all) }
|
||||
sub multisplit($str, @seps) { $str.split(/ ||@seps /, :v) }
|
||||
|
||||
my @chunks = multisplit( 'a!===b=!=c==d', < == != = > );
|
||||
|
||||
|
|
|
|||
11
Task/Multisplit/PowerShell/multisplit.psh
Normal file
11
Task/Multisplit/PowerShell/multisplit.psh
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
$string = "a!===b=!=c"
|
||||
$separators = [regex]"(==|!=|=)"
|
||||
|
||||
$matchInfo = $separators.Matches($string) |
|
||||
Select-Object -Property Index, Value |
|
||||
Group-Object -Property Value |
|
||||
Select-Object -Property @{Name="Separator"; Expression={$_.Name}},
|
||||
Count,
|
||||
@{Name="Position" ; Expression={$_.Group.Index}}
|
||||
|
||||
$matchInfo
|
||||
|
|
@ -1,26 +1,26 @@
|
|||
/*REXX program splits a string based on different separator strings.*/
|
||||
parse arg ? /*get string from command line. */
|
||||
if ?=='' then ? = "a!===b=!=c" /*None specified? Use default.*/
|
||||
say 'old string='? /*echo the old string to screen. */
|
||||
zz = '0'x /*null char, can be most anything*/
|
||||
seps = '== != =' /*a list of seperators to be used*/
|
||||
/* [↓] process tokens in SEPS.*/
|
||||
do j=1 for words(seps) /*parse string with all the seps.*/
|
||||
sep=word(seps,j) /*pick a separator to use now. */
|
||||
/* [↓] process chars in the sep*/
|
||||
do k=1 for length(sep) /*parse for various sep versions.*/
|
||||
sep=strip(insert(zz,sep,k),,zz) /*allow imbedded "nulls" in sep. */
|
||||
?=changestr(sep,?,zz) /* ··· but not trailing "nulls". */
|
||||
/* [↓] process strings in input*/
|
||||
do until ?==??; ??=? /*keep changing until no more chg*/
|
||||
?=changestr(zz || zz, ?, zz) /*reduce replicated "nulls". */
|
||||
end /*until···*/
|
||||
/* [↓] use BIF or external prog.*/
|
||||
sep=changestr(zz, sep, '') /*remove true nulls from the sep.*/
|
||||
end /*k*/
|
||||
end /*j*/
|
||||
/*REXX program splits a (character) string based on different separator delimiters.*/
|
||||
parse arg $ /*obtain optional string from the C.L. */
|
||||
if $='' then $= "a!===b=!=c" /*None specified? Then use the default*/
|
||||
say 'old string:' $ /*display the old string to the screen.*/
|
||||
null= '0'x /*null char. It can be most anything.*/
|
||||
seps= '== != =' /*list of separator strings to be used.*/
|
||||
/* [↓] process the tokens in SEPS. */
|
||||
do j=1 for words(seps) /*parse the string with all the seps. */
|
||||
sep=word(seps,j) /*pick a separator to use in below code*/
|
||||
/* [↓] process characters in the sep.*/
|
||||
do k=1 for length(sep) /*parse for various separator versions.*/
|
||||
sep=strip(insert(null, sep, k), , null) /*allow imbedded "nulls" in separator, */
|
||||
$=changestr(sep, $, null) /* ··· but not trailing "nulls". */
|
||||
/* [↓] process strings in the input. */
|
||||
do until $==old; old=$ /*keep changing until no more changes. */
|
||||
$=changestr(null || null, $, null) /*reduce replicated "nulls" in string. */
|
||||
end /*until*/
|
||||
/* [↓] use BIF or external program.*/
|
||||
sep=changestr(null, sep, '') /*remove true nulls from the separator.*/
|
||||
end /*k*/
|
||||
end /*j*/
|
||||
|
||||
showNull = ' {} ' /*one more thing, display the ···*/
|
||||
?=changestr(zz,?,showNull) /* ··· showing of "null" chars. */
|
||||
say 'new string='? /*now, display the new string. */
|
||||
/*stick a fork in it, we're done.*/
|
||||
showNull= ' {} ' /*just one more thing, display the ··· */
|
||||
$=changestr(null, $, showNull) /* ··· showing of "null" chars. */
|
||||
say 'new string:' $ /*now, display the new string to term. */
|
||||
/*stick a fork in it, we're all done. */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue