Jump to content

Tokenize a string: Difference between revisions

m (Changed over to works with template)
Line 244:
 
=={{header|Pop11}}==
NaturalThe 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
lvars str='Hello,How,Are,You,Today';
lvars ls = [], i, j = 1;
;;; Iterate over string
lvars ls = [], i, j = 1;
for i from 1 to length(str) do
;;; If comma
Cookies help us deliver our services. By using our services, you agree to our use of cookies.