Split a character string based on change of character: Difference between revisions

Content added Content deleted
m (→‎{{header|Lua}}: Simply scan difference in reverse order and insert delimiter in place)
Line 697: Line 697:
{{out}}
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
<pre>g, HHH, 5, YY, ++, ///, \</pre>

'''Alternative:'''
Simply scan difference in reverse order and insert delimiter in place, the loop counter i will not update with length of s.
<lang lua>function splitdiff(s)
for i=#s,2,-1 do
if s:sub(i,i)~=s:sub(i-1,i-1) then
s = s:sub(1,i-1)..', '.. s:sub(i,-1)
end
end
return s
end</lang>


=={{header|Mathematica}}==
=={{header|Mathematica}}==