Tokenize a string: Difference between revisions
Content added Content deleted
m (Changed over to works with template) |
|||
Line 244: | Line 244: | ||
=={{header|Pop11}}== |
=={{header|Pop11}}== |
||
The natural solution in Pop11 uses lists. |
|||
There are built in libraries for tokenising strings, illustrated below, along with code that the user could create for the task. |
|||
First show some uses of the built in procedure sys_parse_string |
|||
;;; Make pop-11 print strings with quotes |
|||
true -> pop_pr_quotes; |
|||
;;; |
|||
;;; Create a string of tokens using comma as token separator |
|||
lvars str='Hello,How,Are,You,Today'; |
|||
;;; |
|||
;;; Make a list of strings by applying sys_parse_string |
|||
;;; to str, using the character `,` as separator (the default |
|||
;;; separator, if none is provided, is the space character). |
|||
lvars strings; |
|||
[% sys_parse_string(str, `,`) %] -> strings; |
|||
;;; |
|||
;;; print the list of strings |
|||
strings => |
|||
** ['Hello' 'How' 'Are' 'You' 'Today'] |
|||
If {% ... %} were used instead of [% ... %] the result would be |
|||
a vector (i.e. array) of strings rather than a list of strings. |
|||
{% sys_parse_string(str, `,`) %} -> strings; |
|||
;;; print the vector |
|||
strings => |
|||
** {'Hello' 'How' 'Are' 'You' 'Today'} |
|||
It is also possible to give sys_parse_string a 'conversion' procedure, which is applied to each of the tokens. |
|||
E.g. it could be used to produce a vector of numbers, using the conversion procedure 'strnumber', which converts a string to a number: |
|||
lvars numbers; |
|||
{% sys_parse_string('100 101 102 103 99.9 99.999', strnumber) %} -> numbers; |
|||
;;; the result is a vector containing integers and floats, |
|||
;;; which can be printed thus: |
|||
numbers => |
|||
** {100 101 102 103 99.9 99.999} |
|||
Using lower level pop-11 facilities to tokenise the string: |
|||
;;; Declare and initialize variables |
;;; Declare and initialize variables |
||
lvars str='Hello,How,Are,You,Today'; |
lvars str='Hello,How,Are,You,Today'; |
||
⚫ | |||
;;; Iterate over string |
;;; Iterate over string |
||
⚫ | |||
for i from 1 to length(str) do |
for i from 1 to length(str) do |
||
;;; If comma |
;;; If comma |