Tokenize a string: Difference between revisions

Content added Content deleted
mNo edit summary
m (→‎{{header|REXX}}: added wording to a REXX section header, added a better fence for the output, added/changed whitespace and comments, used a template for the output section.)
Line 2,387: Line 2,387:
===version 1===
===version 1===
This REXX version doesn't append a period to the last word in the list.
This REXX version doesn't append a period to the last word in the list.
<lang rexx>/*REXX program separates a string of comma-delimited words, and echoes. */
<lang rexx>/*REXX program separates a string of comma─delimited words, and echoes them ──► terminal*/
sss = 'Hello,How,Are,You,Today' /*words seperated by commas (,). */
original = 'Hello,How,Are,You,Today' /*some words separated by commas (,). */
say 'input string =' sss /*display the original string. */
say 'The input string:' original /*display original string ──► terminal.*/
new=sss /*make a copy of the string. */
new= original /*make a copy of the string. */
/* [↓] string NEW is destroyed. */
do #=1 until new=='' /*keep processing until NEW is empty.*/
do items=1 until new=='' /*keep going until NEW is empty.*/
parse var new @.# ',' new /*parse words delineated by a comma (,)*/
end /*#*/ /* [↑] the new array is named @. */
parse var new a.items ',' new /*parse words delinated by comma.*/
end /*items*/ /* [↑] the array is named A. */
say /* NEW is destructively parsed. [↑] */
say center(' Words in the string ', 40, "═") /*display a nice header for the list. */
do j=1 for # /*display all the words (one per line),*/
say @.j || left(., j\==#) /*maybe append a period (.) to a word. */
end /*j*/ /* [↑] don't append a period if last. */
say center(' End─of─list ', 40, "═") /*display a (EOL) trailer for the list.*/</lang>
{{out|output|text=&nbsp; when using the internal default input:}}
<pre>
The input string: Hello,How,Are,You,Today


═════════ Words in the string ══════════
say; say 'Words in the string:' /*display a header for the list. */

do j=1 for items /*now, display all the words. */
say a.j || left('.', j\==items) /*append period to word, maybe. */
end /*j*/ /* [↑] don't append "." if last.*/

say 'End-of-list.' /*display a trailer for the list.*/
/*stick a fork in it, we're done.*/</lang>
{{out}}
<pre>
input string = Hello,How,Are,You,Today
Words in the string:
Hello.
Hello.
How.
How.
Line 2,414: Line 2,410:
You.
You.
Today
Today
═════════════ End─of─list ══════════════
End-of-list.
</pre>
</pre>


===version 2===
===version 2===
This REXX version won't work if any of the words have an embedded blank (or possible a tab character) in them, as in:

Hello,Betty Sue,How,Are,You,Today
<lang rexx>/*REXX program to separate a string of comma-delimited words and echo */
<lang rexx>/*REXX program to separate a string of comma-delimited words and echo */
sss='Hello,How,Are,You,Today'
sss='Hello,How,Are,You,Today'
Line 2,430: Line 2,429:
End
End
say 'End-of-list.'</lang>
say 'End-of-list.'</lang>
'''output''' is identical to REXX version 1.
'''output''' is similar to REXX version 1.


=={{header|Ring}}==
=={{header|Ring}}==