Tokenize a string: Difference between revisions
Content added Content deleted
(→[[Seed7]]: Added Standard ML section and code) |
No edit summary |
||
Line 153: | Line 153: | ||
echo implode('.', array_keys($arr)); |
echo implode('.', array_keys($arr)); |
||
?> |
?> |
||
==[[Pop11]]== |
|||
[[Category:Pop11]] |
|||
Natural solution in Pop11 uses lists: |
|||
;;; Declare and initialize variables |
|||
lvars str='Hello,How,Are,You,Today'; |
|||
lvars ls = [], i, j = 1; |
|||
;;; Iterate over string |
|||
for i from 1 to length(str) do |
|||
;;; If comma |
|||
if str(i) = `,` then |
|||
;;; Preped word (substring) to list |
|||
cons(substring(j, i - j, str), ls) -> ls; |
|||
i + 1 -> j; |
|||
endif; |
|||
endfor; |
|||
;;; Preped final word (if needed) |
|||
if j <= length(str) then |
|||
cons(substring(j, length(str) - j + 1, str), ls) -> ls; |
|||
endif; |
|||
;;; Reverse the list |
|||
rev(ls) -> ls; |
|||
Since the task requires to use array we convert list to array |
|||
;;; Put list elements and lenght on the stack |
|||
destlist(ls); |
|||
;;; Build a vector from them |
|||
lvars ar = consvector(); |
|||
;;; Display in a loop, putting trailing period |
|||
for i from 1 to length(ar) do |
|||
printf(ar(i), '%s.'); |
|||
endfor; |
|||
printf('\n'); |
|||
We could use list directly for printing: |
|||
for i in ls do |
|||
printf(i, '%s.'); |
|||
endfor; |
|||
so the convertion to vector is purely to satisfy task formulation. |
|||
==[[Python]]== |
==[[Python]]== |