      SUBROUTINE SPLIT(TEXT,SEP,ESC)	!Identifies and prints tokens from within a text.
       CHARACTER*(*) TEXT	!To be scanned.
       CHARACTER*(1) SEP	!The only separator for tokens.
       CHARACTER*(1) ESC	!Miscegnator.
       CHARACTER*(LEN(TEXT)) TOKEN	!Surely sufficient space.
       INTEGER N	!Counts the tokens as they're found.
       INTEGER I	!Steps through the text.
       INTEGER L	!Length of the token so far accumulated.
       LOGICAL ESCAPING	!Miscegnatory state.
        N = 0		!No tokens so far.
        L = 0		!Nor any text for the first.
        ESCAPING = .FALSE.	!And the state is good.
        DO I = 1,LEN(TEXT)	!Step through the text.
          IF (ESCAPING) THEN	!Are we in a mess?
            L = L + 1			!Yes. An ESC character had been seen.
            TOKEN(L:L) = TEXT(I:I)	!So, whatever follows is taken as itself.
            ESCAPING = .FALSE.		!There are no specially-recognised names.
           ELSE			!Otherwise, we're in text to inspect.
            IF (TEXT(I:I).EQ.ESC) THEN	!So, is it a troublemaker?
             ESCAPING = .TRUE.			!Yes! Trouble is to follow.
            ELSE IF (TEXT(I:I).EQ.SEP) THEN	!If instead a separator,
             CALL SPLOT				!Then the token up to it is complete.
            ELSE			!Otherwise, a simple constituent character.
             L = L + 1				!So, count it in.
             TOKEN(L:L) = TEXT(I:I)		!And copy it in.
            END IF			!So much for grist.
          END IF		!So much for that character.
        END DO			!On to the next.
Completes on end-of-text with L > 0, or, if the last character had been SEP, a null token is deemed to be following.
        CALL SPLOT	!Tail end.
       CONTAINS	!Save on having two copies of this code.
        SUBROUTINE SPLOT	!Show the token and scrub.
         N = N + 1			!Another one.
         WRITE (6,1) N,TOKEN(1:L)	!Reveal.
    1    FORMAT ("Token ",I0," >",A,"<")!Fancy layout.
         L = 0				!Prepare for a fresh token.
        END SUBROUTINE SPLOT	!A brief life.
      END SUBROUTINE SPLIT	!And then oblivion.

      PROGRAM POKE

      CALL SPLIT("one^|uno||three^^^^|four^^^|^cuatro|","|","^")

      END


Time millisecondsToRun: [
	Transcript show: 2000 factorial
].


fun time_it (action, arg) = let
  val timer = Timer.startCPUTimer ()
  val _ = action arg
  val times = Timer.checkCPUTimer timer
in
  Time.+ (#usr times, #sys times)
end


program timer_test
	timer clear 1
	timer on 1
	sleep `0'
	timer off 1
	timer list 1
end

. timer_test 1000
   1:      1.01 /        1 =       1.0140


import Foundation

public struct TimeResult {
  public var seconds: Double
  public var nanoSeconds: Double

  public var duration: Double { seconds + (nanoSeconds / 1e9) }

  @usableFromInline
  init(seconds: Double, nanoSeconds: Double) {
    self.seconds = seconds
    self.nanoSeconds = nanoSeconds
  }
}

extension TimeResult: CustomStringConvertible {
  public var description: String {
    return "TimeResult(seconds: \(seconds); nanoSeconds: \(nanoSeconds); duration: \(duration)s)"
  }
}

public struct ClockTimer {
  @inlinable @inline(__always)
  public static func time<T>(_ f: () throws -> T) rethrows -> (T, TimeResult) {
    var tsi = timespec()
    var tsf = timespec()

    clock_gettime(CLOCK_MONOTONIC_RAW, &tsi)
    let res = try f()
    clock_gettime(CLOCK_MONOTONIC_RAW, &tsf)

    let secondsElapsed = difftime(tsf.tv_sec, tsi.tv_sec)
    let nanoSecondsElapsed = Double(tsf.tv_nsec - tsi.tv_nsec)

    return (res, TimeResult(seconds: secondsElapsed, nanoSeconds: nanoSecondsElapsed))
  }
}

func ackermann(m: Int, n: Int) -> Int {
  switch (m, n) {
  case (0, _):
    return n + 1
  case (_, 0):
    return ackermann(m: m - 1, n: 1)
  case (_, _):
    return ackermann(m: m - 1, n: ackermann(m: m, n: n - 1))
  }
}

let (n, t) = ClockTimer.time { ackermann(m: 3, n: 11) }

print("Took \(t.duration)s to calculate ackermann(m: 3, n: 11) = \(n)")

let (n2, t2) = ClockTimer.time { ackermann(m: 4, n: 1) }

print("Took \(t2.duration)s to calculate ackermann(m: 4, n: 1) = \(n2)")


proc sum_n {n} {
    for {set i 1; set sum 0.0} {$i <= $n} {incr i} {set sum [expr {$sum + $i}]}
    return [expr {wide($sum)}]
}

puts [time {sum_n 1e6} 100]
puts [time {} 100]


str: "Hello,How,Are,You,Today"

print join.with:"." split.by:"," str


let text = 'Hello,How,Are,You,Today'
let tokens = text.split(||,||)
print tokens.join(with: '.')


string := "Hello,How,Are,You,Today"
stringsplit, string, string, `,
loop, % string0
{
msgbox % string%A_Index%
}


-- Utility
local function append(t1, t2)
    for _, v in ipairs(t2) do
        table.insert(t1, v)
    end
end

-- Node class
local Node = {}
Node.__index = Node

function Node:order(order)
    local r = {}
    append(r, type(self[order[1]]) == "table" and self[order[1]]:order(order) or {self[order[1]]})
    append(r, type(self[order[2]]) == "table" and self[order[2]]:order(order) or {self[order[2]]})
    append(r, type(self[order[3]]) == "table" and self[order[3]]:order(order) or {self[order[3]]})
    return r
end

function Node:levelorder()
    local levelorder = {}
    local queue = {self}
    while next(queue) do
        local node = table.remove(queue, 1)
        table.insert(levelorder, node[1])
        table.insert(queue, node[2])
        table.insert(queue, node[3])
    end
    return levelorder
end

-- Node creator
local function new(value, left, right)
    return value and setmetatable({
        value,
        (type(left) == "table") and new(unpack(left)) or new(left),
        (type(right) == "table") and new(unpack(right)) or new(right),
        }, Node) or nil
end

-- Example
local tree = new(1, {2, {4, 7}, 5}, {3, {6, 8, 9}})
print("preorder:    " .. table.concat(tree:order({1, 2, 3}), " "))
print("inorder:     " .. table.concat(tree:order({2, 1, 3}), " "))
print("postorder:   " .. table.concat(tree:order({2, 3, 1}), " "))
print("level-order: " .. table.concat(tree:levelorder(), " "))


Sub tokenize(cadena As String, separador As String, escape As String)
    Dim As Integer campo = 1
    Dim As Boolean escapando = false
    Dim As String char
    Print ""; campo; " ";

    For i As Integer = 1 To Len(cadena)
        char = Mid(cadena, i, 1)
        If escapando  Then
            Print char;
            escapando = false
        Else
            Select Case char
            Case separador
                Print
                campo += 1
                Print ""; campo; " ";
            Case escape
                escapando = true
            Case Else
                Print char;
            End Select
        End If
    Next i
    Print
End Sub

tokenize("one^|uno||three^^^^|four^^^|^cuatro|", "|", "^")
Sleep


function benchmark(%times,%function,%a,%b,%c,%d,%e,%f,%g,%h,%i,%j,%k,%l,%m,%n,%o)
{
	if(!isFunction(%function))
	{
		warn("BENCHMARKING RESULT FOR" SPC %function @ ":" NL "Function does not exist.");
		return -1;
	}

	%start = getRealTime();

	for(%i=0; %i < %times; %i++)
	{
		call(%function,%a,%b,%c,%d,%e,%f,%g,%h,%i,%j,%k,%l,%m,%n,%o);
	}

	%end = getRealTime();

	%result = (%end-%start) / %times;

	warn("BENCHMARKING RESULT FOR" SPC %function @ ":" NL %result);

	return %result;
}


SUB cont (n)
    LET sum = 0
    FOR i = 1 TO n
        LET sum = sum+1
    NEXT i
END SUB

LET timestart = TIME
CALL cont (10000000)
LET timedone = TIME

!midnight check:
IF timedone < timestart THEN LET timedone = timedone+86400
LET timeelapsed = (timedone-timestart)*1000
PRINT timeelapsed; "miliseconds."
END


$$ MODE TUSCRIPT
SECTION test
LOOP n=1,999999
rest=MOD (n,1000)
IF (rest==0) Print n
ENDLOOP
ENDSECTION
time_beg=TIME ()
DO test
time_end=TIME ()
interval=TIME_INTERVAL (seconds,time_beg,time_end)
PRINT "'test' start at ",time_beg
PRINT "'test' ends  at ",time_end
PRINT "'test' takes ",interval," seconds"


BEGIN {
  s = "Hello,How,Are,You,Today"
  split(s, arr, ",")
  for(i=1; i < length(arr); i++) {
    printf arr[i] "."
  }
  print
}


100 T$ = "HELLO,HOW,ARE,YOU,TODAY"
110 GOSUB 200"TOKENIZE
120 FOR I = 1 TO N
130     PRINT A$(I) "." ;
140 NEXT
150 PRINT
160 END

200 IF N = 0 THEN DIM A$(256)
210 N = 1
220 A$(N) = "
230 FOR TI = 1 TO LEN(T$)
240     C$ = MID$(T$, TI, 1)
250     T = C$ = ","
260     IF T THEN C$ = "
270     N = N + T
280     IF T THEN A$(N) = C$
290     A$(N) = A$(N) + C$
300 NEXT TI
310 RETURN


@echo off
setlocal enabledelayedexpansion
call :tokenize %1 res
echo %res%
goto :eof

:tokenize
set str=%~1
:loop
for %%i in (%str%) do set %2=!%2!.%%i
set %2=!%2:~1!
goto :eof


Module CheckIt {
      Null=(,)
      Tree=((((Null,7,Null),4,Null),2,(Null,5,Null)),1,(((Null,8,Null),6,(Null,9,Null)),3,Null))

      Module preorder (T) {
            Print "preorder:    ";
            printtree(T)
            Print
            sub printtree(T)
                  Print T#val(1);" ";
                  If len(T#val(0))>0 then printtree(T#val(0))
                  If len(T#val(2))>0 then printtree(T#val(2))
            end sub
      }
      preorder Tree

      Module inorder (T) {
            Print "inorder:     ";
            printtree(T)
            Print
            sub printtree(T)
                  If len(T#val(0))>0 then printtree(T#val(0))
                  Print T#val(1);" ";
                  If len(T#val(2))>0 then printtree(T#val(2))
            end sub
      }
      inorder Tree

      Module postorder (T) {
            Print "postorder:   ";
            printtree(T)
            Print
            sub printtree(T)
                  If len(T#val(0))>0 then printtree(T#val(0))
                  If len(T#val(2))>0 then printtree(T#val(2))
                  Print T#val(1);" ";
            end sub
      }
      postorder Tree

      Module level_order (T) {
            Print "level-order: ";
            Stack New {
                  printtree(T)
                  if empty then exit
                  Read T
                  Loop
            }
            Print
            sub printtree(T)
                  If Len(T)>0 then
                        Print T#val(1);" ";
                        Data T#val(0), T#val(2)
                  end if
            end sub
      }
      level_order Tree
}
CheckIt


package main

import (
	"errors"
	"fmt"
)

func TokenizeString(s string, sep, escape rune) (tokens []string, err error) {
	var runes []rune
	inEscape := false
	for _, r := range s {
		switch {
		case inEscape:
			inEscape = false
			fallthrough
		default:
			runes = append(runes, r)
		case r == escape:
			inEscape = true
		case r == sep:
			tokens = append(tokens, string(runes))
			runes = runes[:0]
		}
	}
	tokens = append(tokens, string(runes))
	if inEscape {
		err = errors.New("invalid terminal escape")
	}
	return tokens, err
}

func main() {
	const sample = "one^|uno||three^^^^|four^^^|^cuatro|"
	const separator = '|'
	const escape = '^'

	fmt.Printf("Input:   %q\n", sample)
	tokens, err := TokenizeString(sample, separator, escape)
	if err != nil {
		fmt.Println("error:", err)
	} else {
		fmt.Printf("Tokens: %q\n", tokens)
	}
}


splitEsc :: (Foldable t1, Eq t) => t -> t -> t1 t -> [[t]]
splitEsc sep esc = reverse . map reverse . snd . foldl process (0, [[]])
  where process (st, r:rs) ch
          | st == 0 && ch == esc               = (1,      r:rs)
          | st == 0 && ch == sep               = (0,   []:r:rs)
          | st == 1 && sep == esc && ch /= sep = (0, [ch]:r:rs)
          | otherwise                          = (0, (ch:r):rs)


Public Declare Function GetTickCount Lib "kernel32.dll" () As Long
Private Function identity(x As Long) As Long
    For j = 0 To 1000
    identity = x
    Next j
End Function
Private Function sum(ByVal num As Long) As Long
    Dim t As Long
    For j = 0 To 1000
    t = num
    For i = 0 To 10000
        t = t + i
    Next i
    Next j
    sum = t
End Function
Private Sub time_it()
    Dim start_time As Long, finis_time As Long
    start_time = GetTickCount
    identity 1
    finis_time = GetTickCount
    Debug.Print "1000 times Identity(1) takes "; (finis_time - start_time); " milliseconds"
    start_time = GetTickCount
    sum 1
    finis_time = GetTickCount
    Debug.Print "1000 times Sum(1) takes "; (finis_time - start_time); " milliseconds"
End Sub


import "./check" for Benchmark

Benchmark.run("a function", 100, true) {
    for (i in 0..1e7) {}
}


include c:\cxpl\codes;
int T0, T1, I;
[T0:= GetTime;
for I:= 1, 1_000_000 do [];
T1:= GetTime;
IntOut(0, T1-T0); Text(0, " microseconds^M^J");
]


sub count(n)
	local i

	for i = 1 to n
	next i
end sub

count(1000000)

print peek("millisrunning"), " milliseconds"

t0 = peek("millisrunning")
count(10000000)
print peek("millisrunning")-t0, " milliseconds"


t:=Time.Clock.time; Atomic.sleep(3); (Time.Clock.time - t).println();


( "Hello,How,Are,You,Today":?String
& :?ReverseList
&   whl
  ' ( @(!String:?element "," ?String)
    & !element !ReverseList:?ReverseList
    )
& !String:?List
&   whl
  ' ( !ReverseList:%?element ?ReverseList
    & (!element.!List):?List
    )
& out$!List
)


#include<string.h>
#include<stdio.h>
#include<stdlib.h>

int main(void)
{
	char *a[5];
	const char *s="Hello,How,Are,You,Today";
	int n=0, nn;

	char *ds=strdup(s);

	a[n]=strtok(ds, ",");
	while(a[n] && n<4) a[++n]=strtok(NULL, ",");

	for(nn=0; nn<=n; ++nn) printf("%s.", a[nn]);
	putchar('\n');

	free(ds);

	return 0;
}


string str = "Hello,How,Are,You,Today";
// or Regex.Split ( "Hello,How,Are,You,Today", "," );
// (Regex is in System.Text.RegularExpressions namespace)
string[] strings = str.Split(',');
Console.WriteLine(String.Join(".", strings));


preorder[a_Integer] := a;
preorder[a_[b__]] := [email protected]{a, preorder /@ {b}};
inorder[a_Integer] := a;
inorder[a_[b_, c_]] := [email protected]{[email protected], a, [email protected]};
inorder[a_[b_]] := [email protected]{[email protected], a}; postorder[a_Integer] := a;
postorder[a_[b__]] := [email protected]{postorder /@ {b}, a};
levelorder[a_] :=
 Flatten[Table[Level[a, {n}], {n, 0, [email protected]}]] /. {b_Integer[__] :>
    b};


tokenize1=: tokenize =: '^|'&$: :(4 : 0)
 'ESC SEP' =. x
 STATE =. 0
 RESULT =. 0 $ a:
 TOKEN =. ''
 for_C. y do.
  if. STATE do.
   TOKEN =. TOKEN , C
   STATE =. 0
  else.
   if. C = ESC do.
    STATE =. 1
   elseif. C = SEP do.
    RESULT =. RESULT , < TOKEN
    TOKEN =. ''
   elseif. do.
    TOKEN =. TOKEN , C
   end.
  end.
 end.
 RESULT =. RESULT , < TOKEN
)


import java.util.*;

public class TokenizeStringWithEscaping {

    public static void main(String[] args) {
        String sample = "one^|uno||three^^^^|four^^^|^cuatro|";
        char separator = '|';
        char escape = '^';

        System.out.println(sample);
        try {
            System.out.println(tokenizeString(sample, separator, escape));
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public static List<String> tokenizeString(String s, char sep, char escape)
            throws Exception {
        List<String> tokens = new ArrayList<>();
        StringBuilder sb = new StringBuilder();

        boolean inEscape = false;
        for (char c : s.toCharArray()) {
            if (inEscape) {
                inEscape = false;
            } else if (c == escape) {
                inEscape = true;
                continue;
            } else if (c == sep) {
                tokens.add(sb.toString());
                sb.setLength(0);
                continue;
            }
            sb.append(c);
        }
        if (inEscape)
            throw new Exception("Invalid terminal escape");

        tokens.add(sb.toString());

        return tokens;
    }
}


#include <string>
#include <sstream>
#include <vector>
#include <iterator>
#include <iostream>
#include <algorithm>
int main()
{
    std::string s = "Hello,How,Are,You,Today";
    std::vector<std::string> v;
    std::istringstream buf(s);
    for(std::string token; getline(buf, token, ','); )
        v.push_back(token);
    copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, "."));
    std::cout << '\n';
}


shared void tokenizeAString() {
	value input = "Hello,How,Are,You,Today";
	value tokens = input.split(','.equals);
	print(".".join(tokens));
}


bundle agent main
{
  reports:
    "${with}" with => join(".", splitstring("Hello,How,Are,You,Today", ",", 99));
}


:- module tree_traversal.
:- interface.

:- import_module io.

:- pred main(io::di, io::uo) is det.

:- implementation.

:- import_module list.

:- type tree(V)
    --->    empty
    ;       node(V, tree(V), tree(V)).

:- pred preorder(pred(V, A, A), tree(V), A, A).
:- mode preorder(pred(in, di, uo) is det, in, di, uo) is det.

preorder(_, empty, !Acc).
preorder(P, node(Value, Left, Right), !Acc) :-
    P(Value, !Acc),
    preorder(P, Left, !Acc),
    preorder(P, Right, !Acc).

:- pred inorder(pred(V, A, A), tree(V), A, A).
:- mode inorder(pred(in, di, uo) is det, in, di, uo) is det.

inorder(_, empty, !Acc).
inorder(P, node(Value, Left, Right), !Acc) :-
    inorder(P, Left, !Acc),
    P(Value, !Acc),
    inorder(P, Right, !Acc).

:- pred postorder(pred(V, A, A), tree(V), A, A).
:- mode postorder(pred(in, di, uo) is det, in, di, uo) is det.

postorder(_, empty, !Acc).
postorder(P, node(Value, Left, Right), !Acc) :-
    postorder(P, Left, !Acc),
    postorder(P, Right, !Acc),
    P(Value, !Acc).

:- pred levelorder(pred(V, A, A), tree(V), A, A).
:- mode levelorder(pred(in, di, uo) is det, in, di, uo) is det.

levelorder(P, Tree, !Acc) :-
    do_levelorder(P, [Tree], !Acc).

:- pred do_levelorder(pred(V, A, A), list(tree(V)), A, A).
:- mode do_levelorder(pred(in, di, uo) is det, in, di, uo) is det.

do_levelorder(_, [], !Acc).
do_levelorder(P, [empty | Xs], !Acc) :-
   do_levelorder(P, Xs, !Acc).
do_levelorder(P, [node(Value, Left, Right) | Xs], !Acc) :-
   P(Value, !Acc),
   do_levelorder(P, Xs ++ [Left, Right], !Acc).

:- func tree = tree(int).

tree =
    node(1,
        node(2,
            node(4,
                node(7, empty, empty),
                empty
            ),
            node(5, empty, empty)
        ),
        node(3,
            node(6,
                node(8, empty, empty),
                node(9, empty, empty)
            ),
            empty
        )
    ).

main(!IO) :-
     io.write_string("preorder:   " ,!IO),
     preorder(print_value, tree, !IO), io.nl(!IO),
     io.write_string("inorder:    " ,!IO),
     inorder(print_value, tree, !IO), io.nl(!IO),
     io.write_string("postorder:  " ,!IO),
     postorder(print_value, tree, !IO), io.nl(!IO),
     io.write_string("levelorder: " ,!IO),
     levelorder(print_value, tree, !IO), io.nl(!IO).

:- pred print_value(V::in, io::di, io::uo) is det.

print_value(V, !IO) :-
    io.print(V, !IO),
    io.write_string(" ", !IO).


function tokenize(s, esc, sep) {
	for (var a=[], t='', i=0, e=s.length; i<e; i+=1) {
		var c = s.charAt(i)
		if (c == esc) t+=s.charAt(++i)
		else if (c != sep) t+=c
		else a.push(t), t=''
	}
	a.push(t)
	return a
}

var s = 'one^|uno||three^^^^|four^^^|^cuatro|'
document.write(s, '
')
for (var a=tokenize(s,'^','|'), i=0; i<a.length; i+=1) document.write(i, ': ', a[i], '
')


# Tokenize the input using the string "escape" as the prefix escape string
def tokenize(separator; escape):

  # Helper functions:
  # mapper/1 is like map/1, but for each element, $e, in the input array,
  # if $e is an array, then it is inserted,
  # otherwise the elements of ($e|f) are inserted.
  def mapper(f): reduce .[] as $e
    ( [];
      if ($e|type) == "array" then . + [$e] else . + ($e | f) end ) ;

  # interpolate x
  def interpolate(x):
    reduce .[] as $i ([]; . +  [$i, x]) | .[0:-1];

  def splitstring(s; twixt):
    if type == "string" then split(s) | interpolate(twixt)
    else .
    end;

  # concatenate sequences of non-null elements:
  def reform:
    reduce .[] as $x ([];
      if $x == null and .[-1] == null then .[0:-1] + ["", null]
      elif $x == null then . + [null]
      elif .[-1] == null then .[0:-1] + [$x]
      else .[0:-1] +  [ .[-1] + $x ]
      end)
    | if .[-1] == null then .[-1] = "" else . end;

  splitstring(escape + escape; [escape])
  | mapper( splitstring( escape + separator; [separator]) )
  | mapper( splitstring( separator; null ) )
  | map( if type == "string" then split(escape) else . end)
  | flatten
  | reform ;


function tokenize2(s::AbstractString, sep::Char, esc::Char)
    SPE = "\ufffe"
    SPF = "\uffff"
    s = replace(s, "$esc$esc", SPE) |>
        s -> replace(s, "$esc$sep", SPF) |>
        s -> last(s) == esc ? string(replace(s[1:end-1], esc, ""), esc) : replace(s, esc, "")
    return map(split(s, sep)) do token
        token = replace(token, SPE, esc)
        return replace(token, SPF, sep)
    end
end

@show tokenize2("one^|uno||three^^^^|four^^^|^cuatro|", '|', '^')


// version 1.1.3

const val SPE = "\ufffe"  // unused unicode char in Specials block
const val SPF = "\uffff"  // ditto

fun tokenize(str: String, sep: Char, esc: Char): List<String> {
    var s = str.replace("$esc$esc", SPE).replace("$esc$sep", SPF)
    s = if (s.last() == esc) // i.e. 'esc' not escaping anything
        s.dropLast(1).replace("$esc", "") + esc
    else
        s.replace("$esc", "")
    return s.split(sep).map { it.replace(SPE, "$esc").replace(SPF, "$sep") }
}

fun main(args: Array<String>) {
    var str = "one^|uno||three^^^^|four^^^|^cuatro|"
    val sep = '|'
    val esc = '^'
    val items = tokenize(str, sep, esc)
    for (item in items) println(if (item.isEmpty()) "(empty)" else item)
}


(apply str (interpose "." (.split #"," "Hello,How,Are,You,Today")))


% This iterator splits the string on a given character,
% and returns each substring in order.
tokenize = iter (s: string, c: char) yields (string)
    while ~string$empty(s) do
        next: int := string$indexc(c, s)
        if next = 0 then
            yield(s)
            break
        else
            yield(string$substr(s, 1, next-1))
            s := string$rest(s, next+1)
        end
    end
end tokenize

start_up = proc ()
    po: stream := stream$primary_output()
    str: string := "Hello,How,Are,You,Today"

    for part: string in tokenize(str, ',') do
        stream$putl(po, part || ".")
    end
end start_up


      identification division.
       program-id. tokenize.

       environment division.
       configuration section.
       repository.
           function all intrinsic.

       data division.
       working-storage section.
       01 period constant as ".".
       01 cmma   constant as ",".

       01 start-with.
          05 value "Hello,How,Are,You,Today".

       01 items.
          05 item pic x(6) occurs 5 times.

       procedure division.
       tokenize-main.
       unstring start-with delimited by cmma
           into item(1) item(2) item(3) item(4) item(5)

       display trim(item(1)) period trim(item(2)) period
               trim(item(3)) period trim(item(4)) period
               trim(item(5))

       goback.
       end program tokenize.


import deques

type
  Node[T] = ref object
    data: T
    left, right: Node[T]

proc newNode[T](data: T; left, right: Node[T] = nil): Node[T] =
  Node[T](data: data, left: left, right: right)

proc preorder[T](n: Node[T]): seq[T] =
  if n.isNil: @[]
  else: @[n.data] & preorder(n.left) & preorder(n.right)

proc inorder[T](n: Node[T]): seq[T] =
  if n.isNil: @[]
  else: inorder(n.left) & @[n.data] & inorder(n.right)

proc postorder[T](n: Node[T]): seq[T] =
  if n.isNil: @[]
  else: postorder(n.left) & postorder(n.right) & @[n.data]

proc levelorder[T](n: Node[T]): seq[T] =
  var queue: Deque[Node[T]]
  queue.addLast(n)
  while queue.len > 0:
    let next = queue.popFirst()
    result.add next.data
    if not next.left.isNil: queue.addLast(next.left)
    if not next.right.isNil: queue.addLast(next.right)

let tree = 1.newNode(
             2.newNode(
               4.newNode(
                 7.newNode),
               5.newNode),
             3.newNode(
               6.newNode(
                 8.newNode,
                 9.newNode)))

echo preorder tree
echo inorder tree
echo postorder tree
echo levelorder tree


-- in some movie script

on tokenize (str, sep, esc)
  l = []
  _player.itemDelimiter = sep
  cnt = str.item.count
  repeat with i = 1 to cnt
    prev = l.getLast() -- can be VOID
    if _trailEscCount(prev, esc) mod 2 then
      l[l.count] = prev.char[1..prev.length-1]&sep&str.item[i]
    else
      l.add(str.item[i])
    end if
  end repeat
  -- remove escape characters from tokens
  cnt = l.count
  repeat with i = 1 to cnt
    l[i] = _removeEsc(l[i], esc)
  end repeat
  return l
end

-- counts number of trailing escape characters
on _trailEscCount (str, esc)
  n = 0
  repeat with i = str.length down to 1
    if str.char[i]=esc then n=n+1
    else exit repeat
  end repeat
  return n
end

-- could be implemented more efficiently by using offset()
on _removeEsc (str, esc)
  cnt = str.length-1
  repeat with i = 1 to cnt
    if str.char[i]=esc then
      delete char i of str
      cnt = cnt-1
    end if
  end repeat
  return str
end


function tokenise (str, sep, esc)
    local strList, word, escaped, ch = {}, "", false
    for pos = 1, #str do
        ch = str:sub(pos, pos)
        if ch == esc then
            if escaped then
                word = word .. ch
                escaped = false
            else
                escaped = true
            end
        elseif ch == sep then
            if escaped then
                word = word .. ch
                escaped = false
            else
                table.insert(strList, word)
                word = ""
            end
        else
            escaped = false
            word = word .. ch
        end
    end
    table.insert(strList, word)
    return strList
end

local testStr = "one^|uno||three^^^^|four^^^|^cuatro|"
local testSep, testEsc = "|", "^"
for k, v in pairs(tokenise(testStr, testSep, testEsc)) do
    print(k, v)
end


ClearAll[Tokenize]
Tokenize[str_String, escape_String : "^", sep_String : "|"] :=
 Module[{results = {}, token = "", state = 0, a},
  a = Characters[str];
  Do[
   If[state == 0,
    Switch[c,
     escape,
     state = 1
     ,
     sep,
     AppendTo[results, token];
     token = "";
     ,
     _,
     token = token <> c;
     ]
    ,
    If[state == 1,
     token = token <> c;
     state = 0;
     ]
    ]

   ,
   {c, a}
   ];
  AppendTo[results, token];
  results
 ]
Tokenize["one^|uno||three^^^^|four^^^|^cuatro|"]


import streams

proc tokenize(s: Stream, sep: static[char] = '|', esc: static[char] = '^'): seq[string] =
  var buff = ""
  while not s.atEnd():
    let c = s.readChar
    case c
    of sep:
      result.add buff
      buff = ""
    of esc:
      buff.add s.readChar
    else:
      buff.add c
  result.add buff

for i, s in tokenize(newStringStream "one^|uno||three^^^^|four^^^|^cuatro|"):
    echo i, ":", s


arr = "Hello,How,Are,You,Today".split ","
console.log arr.join "."


<cfoutput>
  <cfset wordListTag = "Hello,How,Are,You,Today">
  #Replace( wordListTag, ",", ".", "all" )#
</cfoutput>


(defun comma-split (string)
  (loop for start = 0 then (1+ finish)
        for finish = (position #\, string :start start)
        collecting (subseq string start finish)
        until (null finish)))

(defun write-with-periods (strings)
  (format t "~{~A~^.~}" strings))


use Collection;

class Test {
  function : Main(args : String[]) ~ Nil {
    one := Node->New(1);
    two := Node->New(2);
    three := Node->New(3);
    four := Node->New(4);
    five := Node->New(5);
    six := Node->New(6);
    seven := Node->New(7);
    eight := Node->New(8);
    nine := Node->New(9);

    one->SetLeft(two); one->SetRight(three);
    two->SetLeft(four); two->SetRight(five);
    three->SetLeft(six); four->SetLeft(seven);
    six->SetLeft(eight); six->SetRight(nine);

    "Preorder: "->Print(); Preorder(one);
    "\nInorder: "->Print(); Inorder(one);
    "\nPostorder: "->Print(); Postorder(one);
    "\nLevelorder: "->Print(); Levelorder(one);
    "\n"->Print();
  }

  function : Preorder(node : Node) ~ Nil {
    if(node <> Nil) {
      System.IO.Console->Print(node->GetData())->Print(", ");
      Preorder(node->GetLeft());
      Preorder(node->GetRight());
    };
  }

  function : Inorder(node : Node) ~ Nil {
    if(node <> Nil) {
      Inorder(node->GetLeft());
      System.IO.Console->Print(node->GetData())->Print(", ");
      Inorder(node->GetRight());
    };
  }

  function : Postorder(node : Node) ~ Nil {
    if(node <> Nil) {
      Postorder(node->GetLeft());
      Postorder(node->GetRight());
      System.IO.Console->Print(node->GetData())->Print(", ");
    };
  }

  function : Levelorder(node : Node) ~ Nil {
    nodequeue := Collection.Queue->New();
    if(node <> Nil) {
      nodequeue->Add(node);
    };

    while(nodequeue->IsEmpty() = false) {
      next := nodequeue->Remove()->As(Node);
      System.IO.Console->Print(next->GetData())->Print(", ");
      if(next->GetLeft() <> Nil) {
        nodequeue->Add(next->GetLeft());
      };

      if(next->GetRight() <> Nil) {
        nodequeue->Add(next->GetRight());
      };
    };
  }
}

class Node from BasicCompare {
  @left : Node;
  @right : Node;
  @data : Int;

  New(data : Int) {
    Parent();
    @data := data;
  }

  method : public : GetData() ~ Int {
    return @data;
  }

  method : public : SetLeft(left : Node) ~ Nil {
    @left := left;
  }

  method : public : GetLeft() ~ Node {
    return @left;
  }

  method : public : SetRight(right : Node) ~ Nil {
    @right := right;
  }

  method : public : GetRight() ~ Node {
    return @right;
  }

  method : public : Compare(rhs : Compare) ~ Int {
    right : Node := rhs->As(Node);
    if(@data = right->GetData()) {
      return 0;
    }
    else if(@data < right->GetData()) {
      return -1;
    };

    return 1;
  }
}


let split_with_escaping ~esc ~sep s =
  let len = String.length s in
  let buf = Buffer.create 16 in
  let rec loop i =
    if i = len then [Buffer.contents buf]
    else if s.[i] = esc && i + 1 < len then begin
      Buffer.add_char buf s.[i + 1];
      loop (i + 2)
    end else if s.[i] = sep then begin
      let s = Buffer.contents buf in
      Buffer.clear buf;
      s :: loop (i + 1)
    end else begin
      Buffer.add_char buf s.[i];
      loop (i + 1)
    end
  in
  loop 0


sub tokenize {
    my ($string, $sep, $esc) = (shift, quotemeta shift, quotemeta shift);

    my @fields = split /$esc . (*SKIP)(*FAIL) | $sep/sx, $string, -1;
    return map { s/$esc(.)/$1/gsr } @fields;
}


function tokenize(string s, integer sep, integer esc)
sequence ret = {}
string word = ""
integer skip = 0

    if length(s)!=0 then
        for i=1 to length(s) do
            integer si = s[i]
            if skip then
                word &= si
                skip = 0
            elsif si=esc then
                skip = 1
            elsif si=sep then
                ret = append(ret,word)
                word = ""
            else
                word &= si
            end if
        end for
        ret = append(ret,word)
    end if
    return ret
end function

?tokenize("one^|uno||three^^^^|four^^^|^cuatro|",'|','^')


include "cowgol.coh";
include "strings.coh";

# Tokenize a string. Note: the string is modified in place.
sub tokenize(sep: uint8, str: [uint8], out: [[uint8]]): (length: intptr) is
    length := 0;
    loop
        [out] := str;
        out := @next out;
        length := length + 1;
        while [str] != 0 and [str] != sep loop
            str := @next str;
        end loop;
        if [str] == sep then
            [str] := 0;
            str := @next str;
        else
            break;
        end if;
    end loop;
end sub;

# The string
var string: [uint8] := "Hello,How,Are,You,Today";

# Make a mutable copy
var buf: uint8[64];
CopyString(string, &buf[0]);

# Tokenize the copy
var parts: [uint8][64];
var length := tokenize(',', &buf[0], &parts[0]) as @indexof parts;

# Print each string
var i: @indexof parts := 0;
while i < length loop
    print(parts[i]);
    print(".\n");
    i := i + 1;
end loop;


void main() {
    import std.stdio, std.string;

    "Hello,How,Are,You,Today".split(',').join('.').writeln;
}


type 'a tree = Empty
             | Node of 'a * 'a tree * 'a tree

let rec preorder f = function
    Empty        -> ()
  | Node (v,l,r) -> f v;
                    preorder f l;
                    preorder f r

let rec inorder f = function
    Empty        -> ()
  | Node (v,l,r) -> inorder f l;
                    f v;
                    inorder f r

let rec postorder f = function
    Empty        -> ()
  | Node (v,l,r) -> postorder f l;
                    postorder f r;
                    f v

let levelorder f x =
  let queue = Queue.create () in
    Queue.add x queue;
    while not (Queue.is_empty queue) do
      match Queue.take queue with
          Empty        -> ()
        | Node (v,l,r) -> f v;
                          Queue.add l queue;
                          Queue.add r queue
    done

let tree =
  Node (1,
        Node (2,
              Node (4,
                    Node (7, Empty, Empty),
                    Empty),
              Node (5, Empty, Empty)),
        Node (3,
              Node (6,
                    Node (8, Empty, Empty),
                    Node (9, Empty, Empty)),
              Empty))

let () =
  preorder   (Printf.printf "%d ") tree; print_newline ();
  inorder    (Printf.printf "%d ") tree; print_newline ();
  postorder  (Printf.printf "%d ") tree; print_newline ();
  levelorder (Printf.printf "%d ") tree; print_newline ()


(de tokenize (Str Sep Esc)
   (split
      (make
         (for (L (chop Str)  L)
            (let C (pop 'L)
               (cond
                  ((= C Esc) (link (pop 'L)))
                  ((= C Sep) (link 0))
                  (T (link C)) ) ) ) )
      0 ) )


function Split-String ([string]$String, [char]$Separator, [char]$Escape)
{
    if ($String -notmatch "\$Separator|\$Escape") {return $String}

    [bool]$escaping = $false
    [string]$output = ""

    for ($i = 0; $i -lt $String.Length; $i++)
    {
        [char]$character = $String.Substring($i,1)

        if ($escaping)
        {
            $output += $character
            $escaping = $false
        }
        else
        {
            switch ($character)
            {
                {$_ -eq $Separator} {$output; $output = ""; break}
                {$_ -eq $Escape}    {$escaping = $true    ; break}
                Default             {$output += $character}
            }
        }
    }

    if ($String[-1] -eq $Separator) {[String]::Empty}
}


def token_with_escape(a, escape = '^', separator = '|'):
    '''
        Issue  python -m doctest thisfile.py  to run the doctests.

        >>> print(token_with_escape('one^|uno||three^^^^|four^^^|^cuatro|'))
        ['one|uno', '', 'three^^', 'four^|cuatro', '']
    '''
    result = []
    token = ''
    state = 0
    for c in a:
        if state == 0:
            if c == escape:
                state = 1
            elif c == separator:
                result.append(token)
                token = ''
            else:
                token += c
        elif state == 1:
            token += c
            state = 0
    result.append(token)
    return result


program Tokenize_a_string;

{$APPTYPE CONSOLE}

uses
  System.SysUtils;

var
  Words: TArray<string>;

begin
  Words := 'Hello,How,Are,You,Today'.Split([',']);
  Writeln(string.Join(#10, Words));

  Readln;
end.


var str = "Hello,How,Are,You,Today"
var strings = str.Split(',')
print(values: strings, separator: ".")


!print join "." split "Hello,How,Are,You,Today" ","


Object Class new: Tree(v, l, r)

Tree method: initialize(v, l, r)  v := v l := l r := r ;
Tree method: v   @v ;
Tree method: l   @l ;
Tree method: r   @r ;

Tree method: preOrder(f)
   @v f perform
   @l ifNotNull: [ @l preOrder(f) ]
   @r ifNotNull: [ @r preOrder(f) ] ;

Tree method: inOrder(f)
   @l ifNotNull: [ @l inOrder(f) ]
   @v f perform
   @r ifNotNull: [ @r inOrder(f) ] ;

Tree method: postOrder(f)
   @l ifNotNull: [ @l postOrder(f) ]
   @r ifNotNull: [ @r postOrder(f) ]
   @v f perform ;

Tree method: levelOrder(f)
| c n |
   Channel new self over send drop ->c
   while(c notEmpty) [
      c receive ->n
      n v f perform
      n l dup ifNotNull: [ c send ] drop
      n r dup ifNotNull: [ c send ] drop
      ] ;


#lang racket/base
(require racket/match)

;; Returns a tokenising function based on sep and esc
(define ((tokenise-with-escape sep esc) str)
  (define tsil->string (compose list->string reverse))
  (define (inr rem l-acc acc)
    (match rem
      ['() (if (and (null? acc) (null? l-acc)) null (reverse (cons (tsil->string l-acc) acc)))]
      [(list (== sep)   tl ...) (inr tl null (cons (tsil->string l-acc) acc))]
      [(list (== esc) c tl ...) (inr tl (cons c l-acc) acc)]
      [(list c          tl ...) (inr tl (cons c l-acc) acc)]))
  (inr (string->list str) null null))

;; This is the tokeniser that matches the parameters in the task
(define task-tokeniser (tokenise-with-escape #\| #\^))

(define (report-input-output str)
  (printf "Input:  ~s~%Output: ~s~%~%" str (task-tokeniser str)))

(report-input-output "one^|uno||three^^^^|four^^^|^cuatro|")
(report-input-output "")
(report-input-output "|")
(report-input-output "^")
(report-input-output ".")


sub tokenize ($string, :$sep!, :$esc!) {
    return $string.match(/([ <!before $sep | $esc> . | $esc . ]*)+ % $sep/)\
                  .[0].map(*.subst: /$esc )> ./, '', :g);
}

say "'$_'" for tokenize 'one^|uno||three^^^^|four^^^|^cuatro|', sep => '|', esc => '^';


/*REXX program demonstrates tokenizing and displaying a string with escaping sequences. */
  str = 'one^|uno||three^^^^|four^^^|^cuatro|'   /*the character string to be tokenized.*/
  esc = '^'                                      /* "    escape  character to be used.  */
  sep = '|'                                      /* "  separator     "      "  "   "    */
  out =                                          /* "  output string  (so far).         */
eMode = 0                                        /*a flag,  escape is in progress.      */

  do j=1  for length(str);  _=substr(str, j, 1)  /*parse a single character at a time.  */
  if eMode   then do; out=out || _;  eMode=0;  iterate;  end   /*are we in escape mode? */
  if _==esc  then do;                eMode=1;  iterate;  end   /*is it an escape char ? */
  if _==sep  then do; call show;               iterate;  end   /* "  " a separator char?*/
  out=out || _                                                 /*append the character.  */
  end   /*j*/

if out\=='' | _==sep  then call show             /*handle a residual str or a separator.*/
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
show:  say  '[length'right(length(out),4)"]"   out;             out=;               return


tokenize("one^|uno||three^^^^|four^^^|^cuatro|", "|", "^")

func tokenize(src, sep, esc)
field = 1
escaping = false
see "" + field + " "
for i = 1 to len(src)
    char = substr(src, i, 1)
    if escaping
       see char
       escaping = false
    else
       switch char
              on sep
                 see nl
                 field = field + 1
                 see "" + field + " "
              on esc
                 escaping = true
              other
                 see char
       off
    ok
next
see nl


import system'routines;
import extensions;

public program()
{
    var string := "Hello,How,Are,You,Today";

    string.splitBy:",".forEach:(s)
    {
        console.print(s,".")
    }
}


tokens = String.split("Hello,How,Are,You,Today", ",")
IO.puts Enum.join(tokens, ".")


-module(tok).
-export([start/0]).

start() ->
   Lst = string:tokens("Hello,How,Are,You,Today",","),
   io:fwrite("~s~n", [string:join(Lst,".")]),
   ok.


  one = .Node~new(1);
  two = .Node~new(2);
  three = .Node~new(3);
  four = .Node~new(4);
  five = .Node~new(5);
  six = .Node~new(6);
  seven = .Node~new(7);
  eight = .Node~new(8);
  nine = .Node~new(9);

  one~left = two
  one~right = three
  two~left = four
  two~right = five
  three~left = six
  four~left = seven
  six~left = eight
  six~right = nine

  out = .array~new
  .treetraverser~preorder(one, out);
  say "Preorder:  " out~toString("l", ", ")
  out~empty
  .treetraverser~inorder(one, out);
  say "Inorder:   " out~toString("l", ", ")
  out~empty
  .treetraverser~postorder(one, out);
  say "Postorder: " out~toString("l", ", ")
  out~empty
  .treetraverser~levelorder(one, out);
  say "Levelorder:" out~toString("l", ", ")

::class node
::method init
  expose left right data
  use strict arg data
  left = .nil
  right = .nil

::attribute left
::attribute right
::attribute data

::class treeTraverser
::method preorder class
  use arg node, out
  if node \== .nil then do
      out~append(node~data)
      self~preorder(node~left, out)
      self~preorder(node~right, out)
  end

::method inorder class
  use arg node, out
  if node \== .nil then do
      self~inorder(node~left, out)
      out~append(node~data)
      self~inorder(node~right, out)
  end

::method postorder class
  use arg node, out
  if node \== .nil then do
      self~postorder(node~left, out)
      self~postorder(node~right, out)
      out~append(node~data)
  end

::method levelorder class
  use arg node, out

  if node == .nil then return
  nodequeue = .queue~new
  nodequeue~queue(node)
  loop while \nodequeue~isEmpty
      next = nodequeue~pull
      out~append(next~data)
      if next~left \= .nil then
          nodequeue~queue(next~left)
      if next~right \= .nil then
          nodequeue~queue(next~right)
  end


def tokenize(string, sep, esc)
  sep = Regexp.escape(sep)
  esc = Regexp.escape(esc)
  string.scan(/\G (?:^ | #{sep}) (?: [^#{sep}#{esc}] | #{esc} .)*/x).collect do |m|
    m.gsub(/#{esc}(.)/, '\1').gsub(/^#{sep}/, '')
  end
end

p tokenize('one^|uno||three^^^^|four^^^|^cuatro|', '|', '^')


const SEPARATOR: char = '|';
const ESCAPE: char = '^';
const STRING: &str = "one^|uno||three^^^^|four^^^|^cuatro|";

fn tokenize(string: &str) -> Vec<String> {
    let mut token = String::new();
    let mut tokens: Vec<String> = Vec::new();
    let mut chars = string.chars();
    while let Some(ch) = chars.next() {
        match ch {
            SEPARATOR => {
                tokens.push(token);
                token = String::new();
            },
            ESCAPE => {
                if let Some(next) = chars.next() {
                    token.push(next);
                }
            },
            _ => token.push(ch),
        }
    }
    tokens.push(token);
    tokens
}

fn main() {
    println!("{:#?}", tokenize(STRING));
}


object TokenizeStringWithEscaping0 extends App {

  val (markerSpE,markerSpF) = ("\ufffe" , "\uffff")

  def tokenize(str: String, sep: String, esc: String): Array[String] = {

    val s0 = str.replace( esc + esc, markerSpE).replace(esc + sep, markerSpF)
    val s = if (s0.last.toString == esc) s0.replace(esc, "") + esc else s0.replace(esc, "")
    s.split(sep.head).map (_.replace(markerSpE, esc).replace(markerSpF, sep))
  }

  def str = "one^|uno||three^^^^|four^^^|^cuatro|"

  tokenize(str, "|", "^").foreach(it => println(if (it.isEmpty) "<empty token>" else it))
}


func tokenize(string, sep, esc) {
    var fields = string.split(
        Regex(esc.escape + '.(*SKIP)(*FAIL)|' + sep.escape, 's'), -1
    )
    fields.map{.gsub(Regex(esc.escape + '(.)'), {|s1| s1 }) }
}

tokenize("one^|uno||three^^^^|four^^^|^cuatro|", '|', '^').each { |str|
    say str.dump
}


function split(sequence s, integer c)
    sequence out
    integer first, delim
    out = {}
    first = 1
    while first<=length(s) do
        delim = find_from(c,s,first)
        if delim = 0 then
            delim = length(s)+1
        end if
        out = append(out,s[first..delim-1])
        first = delim + 1
    end while
    return out
end function

sequence s
s = split("Hello,How,Are,You,Today", ',')

for i = 1 to length(s) do
    puts(1, s[i] & ',')
end for


declare
  Tree = n(1
           n(2
             n(4 n(7 e e) e)
             n(5 e e))
           n(3
             n(6 n(8 e e) n(9 e e))
             e))

  fun {Concat Xs}
     {FoldR Xs Append nil}
  end

  fun {Preorder T}
     case T of e then nil
     [] n(V L R) then
        {Concat [[V]
                 {Preorder L}
                 {Preorder R}]}
     end
  end

  fun {Inorder T}
     case T of e then nil
     [] n(V L R) then
        {Concat [{Inorder L}
                 [V]
                 {Inorder R}]}
     end
  end

  fun {Postorder T}
     case T of e then nil
     [] n(V L R) then
        {Concat [{Postorder L}
                 {Postorder R}
                 [V]]}
     end
  end

  local
     fun {Collect Queue}
        case Queue of nil then nil
        [] e|Xr then {Collect Xr}
        [] n(V L R)|Xr then
           V|{Collect {Append Xr [L R]}}
        end
     end
  in
     fun {Levelorder T}
        {Collect [T]}
     end
  end
in
  {Show {Preorder Tree}}
  {Show {Inorder Tree}}
  {Show {Postorder Tree}}
  {Show {Levelorder Tree}}


SIMSET
BEGIN

    LINK CLASS ITEM(TXT); TEXT TXT;;

    REF(HEAD) PROCEDURE SPLIT(TXT, SEP, ESC); TEXT TXT; CHARACTER SEP, ESC;
    BEGIN
        REF(HEAD) PARTS;
        CHARACTER CH;
        TEXT PART;

        PART :- BLANKS(TXT.LENGTH);
        PARTS :- NEW HEAD;
        TXT.SETPOS(1);
        WHILE TXT.MORE DO BEGIN
            CH := TXT.GETCHAR;
            IF CH = ESC THEN BEGIN
                IF TXT.MORE THEN BEGIN
                    CH := TXT.GETCHAR;
                    PART.PUTCHAR(CH);
                END ELSE BEGIN
                    ERROR("SPLIT: ESCAPE CHAR AT END OF STRING");
                END;
            END ELSE IF CH = SEP THEN BEGIN
                NEW ITEM(COPY(PART.SUB(1,PART.POS-1))).INTO(PARTS);
                PART.SETPOS(1);
            END ELSE BEGIN
                PART.PUTCHAR(CH);
            END;
        END;
        NEW ITEM(COPY(PART.SUB(1,PART.POS-1))).INTO(PARTS);

        SPLIT :- PARTS;
    END SPLIT;

    TEXT EXAMPLE;
    REF(HEAD) RESULT;
    REF(ITEM) PART;
    INTEGER NO;

    FOR EXAMPLE :- "ONE^|UNO||THREE^^^^|FOUR^^^|^CUATRO|" DO
    BEGIN
        OUTTEXT("INPUT: '");
        OUTTEXT(EXAMPLE);
        OUTTEXT("'");
        OUTIMAGE;
        RESULT :- SPLIT(EXAMPLE, '|', '^');
        PART :- RESULT.FIRST;
        NO := 0;
        WHILE PART =/= NONE DO
        BEGIN
            NO := NO + 1;
            OUTTEXT("PART");
            OUTINT(NO, 0);
            OUTTEXT(": '");
            OUTTEXT(PART.TXT);
            OUTTEXT("'");
            OUTIMAGE;
            PART :- PART.SUC;
        END;
    END;

END.


* Program: tokenize_with_escape.sbl
* To run: sbl tokenize_with_escape.sbl
* Description: Tokenize a string with escaping
* Comment: Tested using the Spitbol for Linux version of SNOBOL4

	lf = substr(&alphabet,11,1) ;* New line or line feed

* Function tokenize will break parts out of a string, which are
* separated by c, which defaults to a comma, into
* an array. Parameter kp=1 to keep null parts, which is the default,
* and 0 to discard.
	define('tokenize(s,c,kp)tokenizepat,part,t,i,j')
	:(tokenize_end)
tokenize
	c = (ident(c) ',', substr(c,1,1)) :f(freturn)
	kp = (ident(kp) 1, eq(kp,0) 0, 1) :f(freturn)
	t = table()
	tokenizepat = breakx(c) . part c | (len(1) rem) . part
	s ? eq(kp,1) rtab(1) c = s c
tokenize1
	s ? tokenizepat = "" :f(tokenize2)
	t[i = eq(kp,0) differ(part) i + 1] = part
	t[i = eq(kp,1) i + 1] = part
	:(tokenize1)
tokenize2
	tokenize = array(i) :f(errr)
	j = 0
tokenize3	tokenize[j = lt(j,i) j + 1] = t[j] :s(tokenize3)
	:(return)
tokenize_end

* Function tokcan will a normalize a string by applying separator and escape
* rules to string ts. Parameter sep is the separator, while esc is the escape
* character. Parameter tesc is the new separator character to substitute for
* parameter sep. It defaults to a comma, ",".
	define('tokcan(ts,sep,esc,tesc)tpat,part1,part2,notany') :(tokcan_end)
tokcan
	tesc = (ident(tesc) ',', substr(tesc,1,1))
	tpat = (breakx(sep esc) . part1
+		(sep | esc sep | esc esc | (esc len(1) . notany)) . part2
+		)
+		| (len(1) rem) . part1

tokcan1
	ts ? tpat = :f(tokcan2)
	part2 = (leq(part2,sep) tesc
+		,leq(part2,esc sep) sep
+		,leq(part2,esc esc) esc
+		,differ(notany) leq(part2,esc notany) notany
+		)
	tokcan = (ident(tokcan) "", tokcan) part1 part2
	:(tokcan1)
tokcan2
	:(return)
tokcan_end

	test_string = "one^|uno||three^^^^|four^^^|^cuatro|"
	sep = "|"
	esc = "^"

	hline = tokcan(test_string,sep,esc) :f(err)

	output = "  Input: " test_string lf
	output = "Output1: " hline lf

	output = "Output2: "
	tokenized = tokenize(hline,",")

p1	output = "'" tokenized[z = z + 1] "'" :s(p1)

END


extension String {
  func tokenize(separator: Character, escape: Character) -> [String] {
    var token = ""
    var tokens = [String]()
    var chars = makeIterator()

    while let char = chars.next() {
      switch char {
      case separator:
        tokens.append(token)
        token = ""
      case escape:
        if let next = chars.next() {
          token.append(next)
        }
      case _:
        token.append(char)
      }
    }

    tokens.append(token)

    return tokens
  }
}

print("one^|uno||three^^^^|four^^^|^cuatro|".tokenize(separator: "|", escape: "^"))


oo::class create tokens {
    constructor {s} {
        puts [coroutine Next my Iter $s]
        oo::objdefine [self] forward next Next
    }
    method Iter {s} {
        yield [info coroutine]
        for {set i 0} {$i < [string length $s]} {incr i} {
            yield [string index $s $i]
        }
        return -code break
    }
}

proc tokenize {s {sep |} {escape ^}} {
    set part ""
    set parts ""
    set iter [tokens new $s]
    while {1} {
        set c [$iter next]
        if {$c eq $escape} {
            append part [$iter next]
        } elseif {$c eq $sep} {
            lappend parts $part
            set part ""
        } else {
            append part $c
        }
    }
    lappend parts $part
    return $parts
}

puts [tokenize one^|uno||three^^^^|four^^^|^cuatro| | ^]


"Hello,How,Are,You,Today" "," split "." join print


/* created by Aykayayciti Earl Lamont Montgomery
April 9th, 2018 */

a = []
a = strSplit("Hello,How,Are,You,Today", ",")
index = 0
start = 0
b = ""
for index in [ start : len(a)-1 : 1 ]
	b = b + a[i] + "."
end

> b


class Main
{
  public static Void main ()
  {
    str := "Hello,How,Are,You,Today"
    words := str.split(',')
    words.each |Str word|
    {
      echo ("${word}. ")
    }
  }
}


sub preorder
{
	my $t = shift or return ();
	return ($t->[0], preorder($t->[1]), preorder($t->[2]));
}

sub inorder
{
	my $t = shift or return ();
	return (inorder($t->[1]), $t->[0], inorder($t->[2]));
}

sub postorder
{
	my $t = shift or return ();
	return (postorder($t->[1]), postorder($t->[2]), $t->[0]);
}

sub depth
{
	my @ret;
	my @a = ($_[0]);
	while (@a) {
		my $v = shift @a or next;
		push @ret, $v->[0];
		push @a, @{$v}[1,2];
	}
	return @ret;
}

my $x = [1,[2,[4,[7]],[5]],[3,[6,[8],[9]]]];

print "pre:   @{[preorder($x)]}\n";
print "in:    @{[inorder($x)]}\n";
print "post:  @{[postorder($x)]}\n";
print "depth: @{[depth($x)]}\n";


prog:   char(sep) *
        char(esc) *
str:    smark
token:  forw/outp
        ( [ch==esc?] char(ch) any(!<<>>) token
        | [ch==sep?] char(ch) outp str
        | any(!<<>>) token );
outp:   parse(( scopy = { <"> 1 <"> * } ));
forw:   peek/chkeof;
peek:   [ch=0] char(ch) fail;
chkeof: ( [ch?] succ | fail );

ch:     0;
sep:    0;
esc:    0;


Private Function tokenize(s As String, sep As String, esc As String) As Collection
    Dim ret As New Collection
    Dim this As String
    Dim skip As Boolean

    If Len(s) <> 0 Then
        For i = 1 To Len(s)
            si = Mid(s, i, 1)
            If skip Then
                this = this & si
                skip = False
            Else
                If si = esc Then
                    skip = True
                Else
                    If si = sep Then
                        ret.Add this
                        this = ""
                    Else
                        this = this & si
                    End If
                End If
            End If
        Next i
        ret.Add this
    End If
    Set tokenize = ret
End Function

Public Sub main()
    Dim out As Collection
    Set out = tokenize("one^|uno||three^^^^|four^^^|^cuatro|", "|", "^")
    Dim outstring() As String
    ReDim outstring(out.Count - 1)
    For i = 0 To out.Count - 1
        outstring(i) = out(i + 1)
    Next i
    Debug.Print Join(outstring, ", ")
End Sub


fn tokenize_string(s string, sep u8, escape u8) ?[]string {
    mut tokens := []string{}
	mut runes := []u8{}
	mut in_escape := false
	for r in s {
		if in_escape {
			in_escape = false
		    runes << r
        } else if r == escape {
			in_escape = true
        } else if r == sep {
			tokens << runes.bytestr()
			runes = runes[..0]
		} else {
            runes << r
        }
	}
	tokens << runes.bytestr()
	if in_escape {
		return error("invalid terminal escape")
	}
	return tokens
}

const sample = "one^|uno||three^^^^|four^^^|^cuatro|"
const separator = `|`
const escape = `^`
fn main() {
	println("Input:   $sample")
	tokens := tokenize_string(sample, separator, escape)?
	println("Tokens: $tokens")
}


(fn string.split [self sep]
  (let [pattern (string.format "([^%s]+)" sep)
        fields {}]
    (self:gsub pattern (fn [c] (tset fields (+ 1 (length fields)) c)))
    fields))

(let [str "Hello,How,Are,You,Today"]
  (print (table.concat (str:split ",") ".")))


: split ( str len separator len -- tokens count )
  here >r 2swap
  begin
    2dup 2,             \ save this token ( addr len )
    2over search        \ find next separator
  while
    dup negate  here 2 cells -  +!  \ adjust last token length
    2over nip /string               \ start next search past separator
  repeat
  2drop 2drop
  r>  here over -   ( tokens length )
  dup negate allot           \ reclaim dictionary
  2 cells / ;                \ turn byte length into token count

: .tokens ( tokens count -- )
  1 ?do dup [email protected] type ." ." cell+ cell+ loop [email protected] type ;

s" Hello,How,Are,You,Today" s" ," split .tokens  \ Hello.How.Are.You.Today


PROGRAM Example

  CHARACTER(23) :: str = "Hello,How,Are,You,Today"
  CHARACTER(5) :: word(5)
  INTEGER :: pos1 = 1, pos2, n = 0, i

  DO
    pos2 = INDEX(str(pos1:), ",")
    IF (pos2 == 0) THEN
       n = n + 1
       word(n) = str(pos1:)
       EXIT
    END IF
    n = n + 1
    word(n) = str(pos1:pos1+pos2-2)
    pos1 = pos2+pos1
 END DO

 DO i = 1, n
   WRITE(*,"(2A)", ADVANCE="NO") TRIM(word(i)), "."
 END DO

END PROGRAM Example


constant VALUE = 1, LEFT = 2, RIGHT = 3

constant tree = {1, {2, {4, {7, 0, 0}, 0},
                        {5, 0, 0}},
                    {3, {6, {8, 0, 0},
                            {9, 0, 0}},
                        0}}

procedure preorder(object tree)
    if sequence(tree) then
        printf(1,"%d ",{tree[VALUE]})
        preorder(tree[LEFT])
        preorder(tree[RIGHT])
    end if
end procedure

procedure inorder(object tree)
    if sequence(tree) then
        inorder(tree[LEFT])
        printf(1,"%d ",{tree[VALUE]})
        inorder(tree[RIGHT])
    end if
end procedure

procedure postorder(object tree)
    if sequence(tree) then
        postorder(tree[LEFT])
        postorder(tree[RIGHT])
        printf(1,"%d ",{tree[VALUE]})
    end if
end procedure

procedure level_order(object tree, sequence more = {})
    if sequence(tree) then
        more &= {tree[LEFT],tree[RIGHT]}
        printf(1,"%d ",{tree[VALUE]})
    end if
    if length(more) > 0 then
        level_order(more[1],more[2..$])
    end if
end procedure

puts(1,"\n preorder:    ")  preorder(tree)
puts(1,"\n inorder:     ")  inorder(tree)
puts(1,"\n postorder:   ")  postorder(tree)
puts(1,"\n level-order: ")  level_order(tree)


var SPE = "\ufffe"  // unused unicode character in Specials block
var SPF = "\uffff"  // ditto

var tokenize = Fn.new { |str, sep, esc|
    str = str.replace(esc + esc, SPE).replace(esc + sep, SPF)
    str = (str[-1] == esc) ? str[0...-1].replace(esc, "") + esc : str.replace(esc, "")
    return str.split(sep).map { |s| s.replace(SPE, esc).replace(SPF, sep) }.toList
}

var str = "one^|uno||three^^^^|four^^^|^cuatro|"
var sep = "|"
var esc = "^"
var items = tokenize.call(str, sep, esc)
for (item in items) System.print((item == "") ? "(empty)" : item)


fcn tokenize(str,sep,esc){
   sink:=Sink(String);
   foreach c in (str){
      switch(c){
         case(esc){ sink.write(__cWalker.next()); }  // error if ^EoS
	 case(sep){ sink.write("\xff"); }
	 else     { sink.write(c) }
      }
   }
   sink.close().split("\xff");
}


Public Sub Main()
Dim sString As String[] = Split("Hello,How,Are,You,Today")

Print sString.Join(".")

End


SplitString("Hello,How,Are,You,Today", ",");
# [ "Hello", "How", "Are", "You", "Today" ]

JoinStringsWithSeparator(last, ".");
# "Hello.How.Are.You.Today"


class Node {
    private $left;
    private $right;
    private $value;

    function __construct($value) {
        $this->value = $value;
    }

    public function getLeft() {
        return $this->left;
    }
    public function getRight() {
        return $this->right;
    }
    public function getValue() {
        return $this->value;
    }

    public function setLeft($value) {
        $this->left = $value;
    }
    public function setRight($value) {
        $this->right = $value;
    }
    public function setValue($value) {
        $this->value = $value;
    }
}

class TreeTraversal {

    public function preOrder(Node $n) {
        echo $n->getValue() . " ";
        if($n->getLeft() != null) {
            $this->preOrder($n->getLeft());
        }
        if($n->getRight() != null){
            $this->preOrder($n->getRight());
        }
    }

    public function inOrder(Node $n) {
        if($n->getLeft() != null) {
            $this->inOrder($n->getLeft());
        }
        echo $n->getValue() . " ";
        if($n->getRight() != null){
            $this->inOrder($n->getRight());
        }

    }

    public function postOrder(Node $n) {
        if($n->getLeft() != null) {
            $this->postOrder($n->getLeft());
        }
        if($n->getRight() != null){
            $this->postOrder($n->getRight());
        }
        echo $n->getValue() . " ";
    }

    public function levelOrder($arg) {
        $q[] = $arg;
        while (!empty($q)) {
            $n = array_shift($q);
            echo $n->getValue() . " ";
            if($n->getLeft() != null) {
                $q[] = $n->getLeft();
            }
            if($n->getRight() != null){
                $q[] = $n->getRight();
            }
        }
    }
}

$arr = [];
for ($i=1; $i < 10; $i++) {
    $arr[$i] = new Node($i);
}

$arr[6]->setLeft($arr[8]);
$arr[6]->setRight($arr[9]);
$arr[3]->setLeft($arr[6]);
$arr[4]->setLeft($arr[7]);
$arr[2]->setLeft($arr[4]);
$arr[2]->setRight($arr[5]);
$arr[1]->setLeft($arr[2]);
$arr[1]->setRight($arr[3]);

$tree = new TreeTraversal($arr);

echo "preorder:\t";
$tree->preOrder($arr[1]);
echo "\ninorder:\t";
$tree->inOrder($arr[1]);
echo "\npostorder:\t";
$tree->postOrder($arr[1]);
echo "\nlevel-order:\t";
$tree->levelOrder($arr[1]);


[indent=4]

init
    str:string = "Hello,How,Are,You,Today"
    words:array of string[] = str.split(",")
    joined:string = string.joinv(".", words)
    print joined


package main

import (
    "fmt"
    "strings"
)

func main() {
    s := "Hello,How,Are,You,Today"
    fmt.Println(strings.Join(strings.Split(s, ","), "."))
}


(de preorder (Node Fun)
   (when Node
      (Fun (car Node))
      (preorder (cadr Node) Fun)
      (preorder (caddr Node) Fun) ) )

(de inorder (Node Fun)
   (when Node
      (inorder (cadr Node) Fun)
      (Fun (car Node))
      (inorder (caddr Node) Fun) ) )

(de postorder (Node Fun)
   (when Node
      (postorder (cadr Node) Fun)
      (postorder (caddr Node) Fun)
      (Fun (car Node)) ) )

(de level-order (Node Fun)
   (for (Q (circ Node)  Q)
      (let N (fifo 'Q)
         (Fun (car N))
         (and (cadr N) (fifo 'Q @))
         (and (caddr N) (fifo 'Q @)) ) ) )

(setq *Tree
   (1
      (2 (4 (7)) (5))
      (3 (6 (8) (9))) ) )

(for Order '(preorder inorder postorder level-order)
   (prin (align -13 (pack Order ":")))
   (Order *Tree printsp)
   (prinl) )


{-# OPTIONS_GHC -XOverloadedStrings #-}
import Data.Text (splitOn,intercalate)
import qualified Data.Text.IO as T (putStrLn)

main = T.putStrLn . intercalate "." $ splitOn "," "Hello,How,Are,You,Today"


CHARACTER string="Hello,How,Are,You,Today", list

nWords = INDEX(string, ',', 256) + 1
maxWordLength = LEN(string) - 2*nWords
ALLOCATE(list, nWords*maxWordLength)

DO i = 1, nWords
  EDIT(Text=string, SePaRators=',', item=i, WordEnd, CoPyto=CHAR(i, maxWordLength, list))
ENDDO

DO i = 1, nWords
  WRITE(APPend) TRIM(CHAR(i, maxWordLength, list)), '.'
ENDDO


procedure main()
   A := []
   "Hello,How,Are,You,Today" ? {
      while put(A, 1(tab(upto(',')),=","))
      put(A,tab(0))
      }
   every writes(!A,".")
   write()
end


tree :-
	Tree= [1,
	        [2,
		   [4,
		     [7, nil, nil],
		     nil],
		   [5, nil, nil]],
	        [3,
		 [6,
		   [8, nil, nil],
		   [9,nil, nil]],
		 nil]],

	write('preorder    : '), preorder(Tree), nl,
	write('inorder     : '), inorder(Tree), nl,
	write('postorder   : '), postorder(Tree), nl,
	write('level-order : '), level_order([Tree]).

preorder(nil).
preorder([Node, FG, FD]) :-
	format('~w ', [Node]),
	preorder(FG),
	preorder(FD).

inorder(nil).
inorder([Node, FG, FD]) :-
	inorder(FG),
	format('~w ', [Node]),
	inorder(FD).

postorder(nil).
postorder([Node, FG, FD]) :-
	postorder(FG),
	postorder(FD),
	format('~w ', [Node]).

level_order([]).

level_order(A) :-
	level_order_(A, U-U, S),
	level_order(S).

level_order_([], S-[],S).

level_order_([[Node, FG, FD] | T], CS, FS) :-
	format('~w ', [Node]),
	append_dl(CS, [FG, FD|U]-U, CS1),
	level_order_(T, CS1, FS).

level_order_([nil | T], CS, FS) :-
	level_order_(T, CS, FS).

append_dl(X-Y, Y-Z, X-Z).


"Hello,How,Are,You,Today" split(",") join(".") println


String toTokenize = "Hello,How,Are,You,Today";
System.out.println(String.join(".", toTokenize.split(",")));


Structure node
  value.i
  *left.node
  *right.node
EndStructure

Structure queue
  List q.i()
EndStructure

DataSection
  tree:
  Data.s "1(2(4(7),5),3(6(8,9)))"
EndDataSection

;Convenient routine to interpret string data to construct a tree of integers.
Procedure createTree(*n.node, *tPtr.Character)
  Protected num.s, *l.node, *ntPtr.Character

  Repeat
    Select *tPtr\c
      Case '0' To '9'
        num + Chr(*tPtr\c)
      Case '('
        *n\value = Val(num): num = ""
        *ntPtr = *tPtr + 1
        If *ntPtr\c = ','
          ProcedureReturn *tPtr
        Else
          *l = AllocateMemory(SizeOf(node))
          *n\left = *l: *tPtr = createTree(*l, *ntPtr)
        EndIf
      Case ')', ',', #Null
        If num: *n\value = Val(num): EndIf
        ProcedureReturn *tPtr
    EndSelect

    If *tPtr\c = ','
      *l = AllocateMemory(SizeOf(node)):
      *n\right = *l: *tPtr = createTree(*l, *tPtr + 1)
    EndIf
    *tPtr + 1
  ForEver
EndProcedure

Procedure enqueue(List q.i(), element)
  LastElement(q())
  AddElement(q())
  q() = element
EndProcedure

Procedure dequeue(List q.i())
  Protected element
  If FirstElement(q())
    element = q()
    DeleteElement(q())
  EndIf
  ProcedureReturn element
EndProcedure

Procedure onVisit(*n.node)
  Print(Str(*n\value) + " ")
EndProcedure

Procedure preorder(*n.node) ;recursive
  onVisit(*n)
  If *n\left
    preorder(*n\left)
  EndIf
  If *n\right
    preorder(*n\right)
  EndIf
EndProcedure

Procedure inorder(*n.node) ;recursive
  If *n\left
    inorder(*n\left)
  EndIf
  onVisit(*n)
  If *n\right
    inorder(*n\right)
  EndIf
EndProcedure

Procedure postorder(*n.node) ;recursive
  If *n\left
    postorder(*n\left)
  EndIf
  If *n\right
    postorder(*n\right)
  EndIf
  onVisit(*n)
EndProcedure

Procedure levelorder(*n.node)
  Dim q.queue(1)
  Protected readQueue = 1, writeQueue, *currNode.node

  enqueue(q(writeQueue)\q(),*n) ;start queue off with root
  Repeat
    readQueue ! 1: writeQueue ! 1
    While ListSize(q(readQueue)\q())
      *currNode = dequeue(q(readQueue)\q())
      If *currNode\left
        enqueue(q(writeQueue)\q(),*currNode\left)
      EndIf
      If *currNode\right
        enqueue(q(writeQueue)\q(),*currNode\right)
      EndIf
      onVisit(*currNode)
    Wend
  Until ListSize(q(writeQueue)\q()) = 0
EndProcedure

If OpenConsole()
  Define root.node
  createTree(root,?tree)

  Print("preorder: ")
  preorder(root)
  PrintN("")
  Print("inorder: ")
  inorder(root)
  PrintN("")
  Print("postorder: ")
  postorder(root)
  PrintN("")
  Print("levelorder: ")
  levelorder(root)
  PrintN("")

  Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
  Input()
  CloseConsole()
EndIf


s = "Hello,How,Are,You,Today"
a = split(s, ",")
t = join(a, ".")

println("The string \"", s, "\"")
println("Splits into ", a)
println("Reconstitutes to \"", t, "\"")


from collections import namedtuple

Node = namedtuple('Node', 'data, left, right')
tree = Node(1,
            Node(2,
                 Node(4,
                      Node(7, None, None),
                      None),
                 Node(5, None, None)),
            Node(3,
                 Node(6,
                      Node(8, None, None),
                      Node(9, None, None)),
                 None))

def printwithspace(i):
    print(i, end=' ')

def dfs(order, node, visitor):
    if node is not None:
        for action in order:
            if action == 'N':
                visitor(node.data)
            elif action == 'L':
                dfs(order, node.left, visitor)
            elif action == 'R':
                dfs(order, node.right, visitor)

def preorder(node, visitor = printwithspace):
    dfs('NLR', node, visitor)

def inorder(node, visitor = printwithspace):
    dfs('LNR', node, visitor)

def postorder(node, visitor = printwithspace):
    dfs('LRN', node, visitor)

def ls(node, more, visitor, order='TB'):
    "Level-based Top-to-Bottom or Bottom-to-Top tree search"
    if node:
        if more is None:
            more = []
        more += [node.left, node.right]
    for action in order:
        if action == 'B' and more:
            ls(more[0], more[1:], visitor, order)
        elif action == 'T' and node:
            visitor(node.data)

def levelorder(node, more=None, visitor = printwithspace):
    ls(node, more, visitor, 'TB')

# Because we can
def reverse_preorder(node, visitor = printwithspace):
    dfs('RLN', node, visitor)

def bottom_up_order(node, more=None, visitor = printwithspace, order='BT'):
    ls(node, more, visitor, 'BT')

if __name__ == '__main__':
    w = 10
    for traversal in [preorder, inorder, postorder, levelorder,
                      reverse_preorder, bottom_up_order]:
        if traversal == reverse_preorder:
            w = 20
            print('\nThe generalisation of function dfs allows:')
        if traversal == bottom_up_order:
            print('The generalisation of function ls allows:')
        print(f"{traversal.__name__:>{w}}:", end=' ')
        traversal(tree)
        print()


words: "," \: "Hello,How,Are,You,Today"
"." /: words


( "Hello,How,Are,You,Today" "," ) split len [ get print "." print ] for

nl "End " input


fun main(args: Array<String>) {
    val input = "Hello,How,Are,You,Today"
    println(input.split(',').joinToString("."))
}


(set *tree* [1 [2 [4 [7]]
                  [5]]
               [3 [6 [8]
                     [9]]]])

(define inorder
  []      -> []
  [V]     -> [V]
  [V L]   -> (append (inorder L)
                     [V])
  [V L R] -> (append (inorder L)
                     [V]
                     (inorder R)))

(define postorder
  []      -> []
  [V]     -> [V]
  [V L]   -> (append (postorder L)
                     [V])
  [V L R] -> (append (postorder L)
                     (postorder R)
                     [V]))

(define preorder
  []      -> []
  [V]     -> [V]
  [V L]   -> (append [V]
                     (preorder L))
  [V L R] -> (append [V]
                     (preorder L)
                     (preorder R)))

(define levelorder-0
  []             -> []
  [[]       | Q] -> (levelorder-0 Q)
  [[V | LR] | Q] -> [V | (levelorder-0 (append Q LR))])

(define levelorder
  Node -> (levelorder-0 [Node]))

(preorder (value *tree*))
(postorder (value *tree*))
(inorder (value *tree*))
(levelorder (value *tree*))


#!/bin/ksh

# Tokenize a string

#	# Variables:
#
string="Hello,How,Are,You,Today"
inputdelim=\,		# a comma
outputdelim=\.		# a period

#	# Functions:
#
#	# Function _tokenize(str, indelim, outdelim)
#
function _tokenize {
	typeset _str ; _str="$1"
	typeset _ind ; _ind="$2"
	typeset _outd ; _outd="$3"

	while [[ ${_str} != ${_str/${_ind}/${_outd}} ]]; do
		_str=${_str/${_ind}/${_outd}}
	done

	echo "${_str}"
}

 ######
# main #
 ######

 _tokenize "${string}" "${inputdelim}" "${outputdelim}"


{S.replace , by . in Hello,How,Are,You,Today}.
-> Hello.How.Are.You.Today.


'Hello,How,Are,You,Today ', split '. join .


  [ this ]                  is nil         (   --> [ )

  [ ' [ 1
        [ 2
          [ 4
            [ 7 nil nil ]
            nil ]
          [ 5 nil nil ] ]
        [ 3
          [ 6
            [ 8 nil nil ]
            [ 9 nil nil ] ]
          nil ] ] ]         is tree        (   --> [ )

  [ dup nil = iff drop done
    unpack swap rot
    echo sp
    recurse
    recurse ]               is pre-order   ( [ -->   )

  [ dup nil = iff drop done
    unpack unrot
    recurse
    echo sp
    recurse ]               is in-order    ( [ -->   )

  [ dup nil = iff drop done
    unpack swap
    recurse
    recurse
    echo sp ]               is post-order  ( [ -->   )

  [ queue swap push
    [ dup empty?
        iff drop done
      pop
      dup nil = iff
        drop again
      unpack
      rot echo sp
      dip push push
      again ] ]             is level-order ( [ -->   )

  tree pre-order   cr
  tree in-order    cr
  tree post-order  cr
  tree level-order cr


DATA:
explode/words is text vector
explode/index is number
explode/string is text
explode/length is number
explode/stringlength is number
explode/current-token is text
explode/char is text
explode/separator is text
i is number

PROCEDURE:
# Ask for a sentence
display "Enter a sentence: "
accept explode/string

# Declare explode Subprocedure
# Splits a text into a text vector by a certain delimiter
# Input parameters:
# - explode/string: the string to explode (destroyed)
# - explode/separator: the character used to separate the string (preserved)
# Output parameters:
# - explode/words: vector of splitted words
# - explode/length: length of explode/words
sub-procedure explode
    join explode/string and explode/separator in explode/string
    store length of explode/string in explode/stringlength
    store 0 in explode/index
    store 0 in explode/length
    store "" in explode/current-token
    while explode/index is less than explode/stringlength do
        get character at explode/index from explode/string in explode/char
        if explode/char is equal to explode/separator then
            store explode/current-token in explode/words:explode/length
            add explode/length and 1 in explode/length
            store "" in explode/current-token
        else
            join explode/current-token and explode/char in explode/current-token
        end if
        add explode/index and 1 in explode/index
    repeat
    subtract 1 from explode/length in explode/length
end sub-procedure

# Separate the entered string
store " " in explode/separator
call sub-procedure explode
while i is less than or equal to explode/length do
    display explode/words:i crlf
    add 1 and i in i
repeat


> (set split (string:tokens "Hello,How,Are,You,Today" ","))
("Hello" "How" "Are" "You" "Today")
> (string:join split ".")
"Hello.How.Are.You.Today"


input = "Hello,How,Are,You,Today"
_player.itemDelimiter = ","
output = ""
repeat with i = 1 to input.item.count
  put input.item[i]&"." after output
end repeat
delete the last char of output
put output
-- "Hello.How.Are.You.Today"


to split :str :sep
  output parse map [ifelse ? = :sep ["| |] [?]] :str
end


#lang racket

(define the-tree ; Node: (list <data> <left> <right>)
  '(1 (2 (4 (7 #f #f) #f) (5 #f #f)) (3 (6 (8 #f #f) (9 #f #f)) #f)))

(define (preorder tree visit)
  (let loop ([t tree])
    (when t (visit (car t)) (loop (cadr t)) (loop (caddr t)))))
(define (inorder tree visit)
  (let loop ([t tree])
    (when t (loop (cadr t)) (visit (car t)) (loop (caddr t)))))
(define (postorder tree visit)
  (let loop ([t tree])
    (when t (loop (cadr t)) (loop (caddr t)) (visit (car t)))))
(define (levelorder tree visit)
  (let loop ([trees (list tree)])
    (unless (null? trees)
      ((compose1 loop (curry filter values) append*)
       (for/list ([t trees] #:when t) (visit (car t)) (cdr t))))))

(define (run order)
  (printf "~a:" (object-name order))
  (order the-tree (λ(x) (printf " ~s" x)))
  (newline))
(for-each run (list preorder inorder postorder levelorder))


:- object(spliting).

    :- public(convert/2).
    :- mode(convert(+atom, -atom), one).

    convert(StringIn, StringOut) :-
        atom_chars(StringIn, CharactersIn),
        phrase(split(',', Tokens), CharactersIn),
        phrase(split('.', Tokens), CharactersOut),
        atom_chars(StringOut, CharactersOut).

    split(Separator, [t([Character| Characters])| Tokens]) -->
        [Character], {Character \== Separator}, split(Separator, [t(Characters)| Tokens]).
    split(Separator, [t([])| Tokens]) -->
        [Separator], split(Separator, Tokens).
    split(_, [t([])]) -->
        [].
    % the look-ahead in the next rule prevents adding a spurious separator at the end
    split(_, []), [Character] -->
        [Character].

:- end_object.


function string:split (sep)
    local sep, fields = sep or ":", {}
    local pattern = string.format("([^%s]+)", sep)
    self:gsub(pattern, function(c) fields[#fields+1] = c end)
    return fields
end

local str = "Hello,How,Are,You,Today"
print(table.concat(str:split(","), "."))


Module CheckIt {
	Function Tokenize$(s){
		\\ letter$ pop a string from stack of values
		\\ shift 2 swap top two values on stack of values
		fold1=lambda m=1 ->{
			shift 2 :if m=1 then m=0:drop: push letter$ else push letter$+"."+letter$
		}
		=s#fold$(fold1)
	}
	Print Tokenize$(piece$("Hello,How,Are,You,Today",",")) ="Hello.How.Are.You.Today"   ' true
}
Checkit


class TreeNode {
    has TreeNode $.parent;
    has TreeNode $.left;
    has TreeNode $.right;
    has $.value;

    method pre-order {
        flat gather {
            take $.value;
            take $.left.pre-order if $.left;
            take $.right.pre-order if $.right
        }
    }

    method in-order {
        flat gather {
            take $.left.in-order if $.left;
            take $.value;
            take $.right.in-order if $.right;
        }
    }

    method post-order {
        flat gather {
            take $.left.post-order if $.left;
            take $.right.post-order if $.right;
            take $.value;
        }
    }

    method level-order {
        my TreeNode @queue = (self);
        flat gather while @queue.elems {
            my $n = @queue.shift;
            take $n.value;
            @queue.push($n.left) if $n.left;
            @queue.push($n.right) if $n.right;
        }
    }
}

my TreeNode $root .= new( value => 1,
                    left => TreeNode.new( value => 2,
                            left => TreeNode.new( value => 4, left => TreeNode.new(value => 7)),
                            right => TreeNode.new( value => 5)
                    ),
                    right => TreeNode.new( value => 3,
                             left => TreeNode.new( value => 6,
                                     left => TreeNode.new(value => 8),
                                     right => TreeNode.new(value => 9)
                                     )
                             )
                    );

say "preorder:  ",$root.pre-order.join(" ");
say "inorder:   ",$root.in-order.join(" ");
say "postorder: ",$root.post-order.join(" ");
say "levelorder:",$root.level-order.join(" ");


define(`s',`Hello,How,Are,You,Today')
define(`set',`define(`$1[$2]',`$3')')
define(`get',`defn($1[$2])')
define(`n',0)
define(`fill',
   `set(a,n,$1)`'define(`n',incr(n))`'ifelse(eval($#>1),1,`fill(shift([email protected]))')')
fill(s)
define(`j',0)
define(`show',
   `ifelse(eval(j<n),1,`get(a,j).`'define(`j',incr(j))`'show')')
show


[email protected]["Hello,How,Are,You,Today", "," -> "."]


tree: [1 [2 [4 [7 [] []] []] [5 [] []]] [3 [6 [8 [] []] [9 [] []]] []]]
; "compacted" version
tree: [1 [2 [4 [7  ] ] [5  ]] [3 [6 [8  ] [9  ]] ]]

visit: func [tree [block!]][prin rejoin [first tree " "]]
left: :second
right: :third

preorder: func [tree [block!]][
	if not empty? tree [visit tree]
	attempt [preorder left tree]
	attempt [preorder right tree]
]
prin "preorder:    " preorder tree
print ""

inorder: func [tree [block!]][
	attempt [inorder left tree]
	if not empty? tree [visit tree]
	attempt [inorder right tree]
]
prin "inorder:     " inorder tree
print ""

postorder: func [tree [block!]][
	attempt [postorder left tree]
	attempt [postorder right tree]
	if not empty? tree [visit tree]
]
prin "postorder:   " postorder tree
print ""

queue: []
enqueue: func [tree [block!]][append/only queue tree]
dequeue: func [queue [block!]][take queue]
level-order: func [tree [block!]][
	clear head queue
	queue: enqueue tree
	while [not empty? queue] [
		tree: dequeue queue
		if not empty? tree [visit tree]
		attempt [enqueue left tree]
		attempt [enqueue right tree]
	]
]
prin "level-order: " level-order tree


l: split("Hello,How,Are,You,Today", ",")$
printf(true, "~{~a~^.~}~%", l)$


output = ""
for word in (filterString "Hello,How,Are,You,Today" ",") do
(
    output += (word + ".")
)
format "%\n" output


:- module string_tokenize.
:- interface.

:- import_module io.
:- pred main(io::di, io::uo) is det.

:- implementation.
:- import_module list, string.

main(!IO) :-
    Tokens = string.split_at_char((','), "Hello,How,Are,You,Today"),
    io.write_list(Tokens, ".", io.write_string, !IO),
    io.nl(!IO).


"Hello,How,Are,You,Today" "," split "." join print


tokens = "Hello,How,Are,You,Today".split(",")
print tokens.join(".")


sep	IS	','
EOS	IS	0
NL	IS	10

// main registers
p	IS	$255
tp	GREG
c	GREG
t	GREG

	LOC	Data_Segment
	GREG	@
Text	BYTE	"Hello,How,Are,You,Today",EOS
token	BYTE	0
eot	IS	@+255

	LOC	#100	% main () {
Main	LDA	p,Text		%
	LDA	tp,token	% initialize pointers
2H	LDBU	c,p		% DO  get char
	BZ	c,5F		%  break if char == EOS
	CMP	t,c,sep		%  if char != sep then
	PBNZ	t,3F		%     store char
	SET	t,NL		%  terminate token with NL,EOS
	STBU	t,tp
	SET	t,EOS
	INCL	tp,1
	STBU	t,tp
	JMP	4F		%  continue

3H	STBU	c,tp		%  store char
4H	INCL	tp,1		%  update pointers
	INCL	p,1
	JMP	2B		% LOOP

5H	SET	t,NL		% terminate last token and buffer
	STBU	t,tp
	SET	t,EOS
	INCL	tp,1
	STBU	t,tp
%  next part is not really necessary
%  program runs only once
%	INCL	tp,1		% terminate buffer
%	STBU	t,tp

	LDA	tp,token	% reset token pointer
				% REPEAT
2H	ADD	p,tp,0		%    start of token
	TRAP	0,Fputs,StdOut	%    output token
	ADD	tp,tp,p
	INCL	tp,1		%    step to next token
	LDBU	t,tp
	PBNZ	t,2B		% UNTIL EOB(uffer)
	TRAP	0,Halt,0


MODULE Tokenize EXPORTS Main;

IMPORT IO, TextConv;

TYPE Texts = REF ARRAY OF TEXT;

VAR tokens: Texts;
    string := "Hello,How,Are,You,Today";
    sep := SET OF CHAR {','};

BEGIN
  tokens := NEW(Texts, TextConv.ExplodedSize(string, sep));
  TextConv.Explode(string, tokens^, sep);
  FOR i := FIRST(tokens^) TO LAST(tokens^) DO
    IO.Put(tokens[i] & ".");
  END;
  IO.Put("\n");
END Tokenize.


BinaryTreeNode = Struct.new(:value, :left, :right) do
  def self.from_array(nested_list)
    value, left, right = nested_list
    if value
      self.new(value, self.from_array(left), self.from_array(right))
    end
  end

  def walk_nodes(order, &block)
    order.each do |node|
      case node
      when :left  then left && left.walk_nodes(order, &block)
      when :self  then yield self
      when :right then right && right.walk_nodes(order, &block)
      end
    end
  end

  def each_preorder(&b)  walk_nodes([:self, :left, :right], &b) end
  def each_inorder(&b)   walk_nodes([:left, :self, :right], &b) end
  def each_postorder(&b) walk_nodes([:left, :right, :self], &b) end

  def each_levelorder
    queue = [self]
    until queue.empty?
      node = queue.shift
      yield node
      queue << node.left if node.left
      queue << node.right if node.right
    end
  end
end

root = BinaryTreeNode.from_array [1, [2, [4, 7], [5]], [3, [6, [8], [9]]]]

BinaryTreeNode.instance_methods.select{|m| m=~/.+order/}.each do |mthd|
  printf "%-11s ", mthd[5..-1] + ':'
  root.send(mthd) {|node| print "#{node.value} "}
  puts
end


TOKENS
 NEW I,J,INP
 SET INP="Hello,how,are,you,today"
 NEW I FOR I=1:1:$LENGTH(INP,",") SET INP(I)=$PIECE(INP,",",I)
 NEW J FOR J=1:1:I WRITE INP(J) WRITE:J'=I "."
 KILL I,J,INP  // Kill is optional. "New" variables automatically are killed on "Quit"
 QUIT


for word in "Hello,How,Are,You,Today".split(",")
        print word + "."
end


using System;
using System.Console;
using Nemerle.Utility.NString;

module Tokenize
{
    Main() : void
    {
        def cswords = "Hello,How,Are,You,Today";
        WriteLine(Concat(".", $[s | s in cswords.Split(',')]));
        // Split() produces an array while Concat() consumes a list
        // a quick in place list comprehension takes care of that
    }
}


#![feature(box_syntax, box_patterns)]

use std::collections::VecDeque;

#[derive(Debug)]
struct TreeNode<T> {
    value: T,
    left: Option<Box<TreeNode<T>>>,
    right: Option<Box<TreeNode<T>>>,
}

enum TraversalMethod {
    PreOrder,
    InOrder,
    PostOrder,
    LevelOrder,
}

impl<T> TreeNode<T> {
    pub fn new(arr: &[[i8; 3]]) -> TreeNode<i8> {

        let l = match arr[0][1] {
            -1 => None,
            i @ _ => Some(Box::new(TreeNode::<i8>::new(&arr[(i - arr[0][0]) as usize..]))),
        };
        let r = match arr[0][2] {
            -1 => None,
            i @ _ => Some(Box::new(TreeNode::<i8>::new(&arr[(i - arr[0][0]) as usize..]))),
        };

        TreeNode {
            value: arr[0][0],
            left: l,
            right: r,
        }
    }

    pub fn traverse(&self, tr: &TraversalMethod) -> Vec<&TreeNode<T>> {
        match tr {
            &TraversalMethod::PreOrder => self.iterative_preorder(),
            &TraversalMethod::InOrder => self.iterative_inorder(),
            &TraversalMethod::PostOrder => self.iterative_postorder(),
            &TraversalMethod::LevelOrder => self.iterative_levelorder(),
        }
    }

    fn iterative_preorder(&self) -> Vec<&TreeNode<T>> {
        let mut stack: Vec<&TreeNode<T>> = Vec::new();
        let mut res: Vec<&TreeNode<T>> = Vec::new();

        stack.push(self);
        while !stack.is_empty() {
            let node = stack.pop().unwrap();
            res.push(node);
            match node.right {
                None => {}
                Some(box ref n) => stack.push(n),
            }
            match node.left {
                None => {}
                Some(box ref n) => stack.push(n),
            }
        }
        res
    }

    // Leftmost to rightmost
    fn iterative_inorder(&self) -> Vec<&TreeNode<T>> {
        let mut stack: Vec<&TreeNode<T>> = Vec::new();
        let mut res: Vec<&TreeNode<T>> = Vec::new();
        let mut p = self;

        loop {
            // Stack parents and right children while left-descending
            loop {
                match p.right {
                    None => {}
                    Some(box ref n) => stack.push(n),
                }
                stack.push(p);
                match p.left {
                    None => break,
                    Some(box ref n) => p = n,
                }
            }
            // Visit the nodes with no right child
            p = stack.pop().unwrap();
            while !stack.is_empty() && p.right.is_none() {
                res.push(p);
                p = stack.pop().unwrap();
            }
            // First node that can potentially have a right child:
            res.push(p);
            if stack.is_empty() {
                break;
            } else {
                p = stack.pop().unwrap();
            }
        }
        res
    }

    // Left-to-right postorder is same sequence as right-to-left preorder, reversed
    fn iterative_postorder(&self) -> Vec<&TreeNode<T>> {
        let mut stack: Vec<&TreeNode<T>> = Vec::new();
        let mut res: Vec<&TreeNode<T>> = Vec::new();

        stack.push(self);
        while !stack.is_empty() {
            let node = stack.pop().unwrap();
            res.push(node);
            match node.left {
                None => {}
                Some(box ref n) => stack.push(n),
            }
            match node.right {
                None => {}
                Some(box ref n) => stack.push(n),
            }
        }
        let rev_iter = res.iter().rev();
        let mut rev: Vec<&TreeNode<T>> = Vec::new();
        for elem in rev_iter {
            rev.push(elem);
        }
        rev
    }

    fn iterative_levelorder(&self) -> Vec<&TreeNode<T>> {
        let mut queue: VecDeque<&TreeNode<T>> = VecDeque::new();
        let mut res: Vec<&TreeNode<T>> = Vec::new();

        queue.push_back(self);
        while !queue.is_empty() {
            let node = queue.pop_front().unwrap();
            res.push(node);
            match node.left {
                None => {}
                Some(box ref n) => queue.push_back(n),
            }
            match node.right {
                None => {}
                Some(box ref n) => queue.push_back(n),
            }
        }
        res
    }
}

fn main() {
    // Array representation of task tree
    let arr_tree = [[1, 2, 3],
                    [2, 4, 5],
                    [3, 6, -1],
                    [4, 7, -1],
                    [5, -1, -1],
                    [6, 8, 9],
                    [7, -1, -1],
                    [8, -1, -1],
                    [9, -1, -1]];

    let root = TreeNode::<i8>::new(&arr_tree);

    for method_label in [(TraversalMethod::PreOrder, "pre-order:"),
                         (TraversalMethod::InOrder, "in-order:"),
                         (TraversalMethod::PostOrder, "post-order:"),
                         (TraversalMethod::LevelOrder, "level-order:")]
                            .iter() {
        print!("{}\t", method_label.1);
        for n in root.traverse(&method_label.0) {
            print!(" {}", n.value);
        }
        print!("\n");
    }
}


(print (join (parse "Hello,How,Are,You,Today" ",") "."))


import strutils

let text = "Hello,How,Are,You,Today"
let tokens = text.split(',')
echo tokens.join(".")


class Parse {
  function : Main(args : String[]) ~ Nil {
    tokens := "Hello,How,Are,You,Today"->Split(",");
    each(i : tokens) {
      tokens[i]->PrintLine();
    };
  }
}


case class IntNode(value: Int, left: Option[IntNode] = None, right: Option[IntNode] = None) {

  def preorder(f: IntNode => Unit) {
    f(this)
    left.map(_.preorder(f)) // Same as: if(left.isDefined) left.get.preorder(f)
    right.map(_.preorder(f))
  }

  def postorder(f: IntNode => Unit) {
    left.map(_.postorder(f))
    right.map(_.postorder(f))
    f(this)
  }

  def inorder(f: IntNode => Unit) {
    left.map(_.inorder(f))
    f(this)
    right.map(_.inorder(f))
  }

  def levelorder(f: IntNode => Unit) {

    def loVisit(ls: List[IntNode]): Unit = ls match {
      case Nil => None
      case node :: rest => f(node); loVisit(rest ++ node.left ++ node.right)
    }

    loVisit(List(this))
  }
}

object TreeTraversal extends App {
  implicit def intNode2SomeIntNode(n: IntNode) = Some[IntNode](n)

  val tree = IntNode(1,
    IntNode(2,
      IntNode(4,
        IntNode(7)),
      IntNode(5)),
    IntNode(3,
      IntNode(6,
        IntNode(8),
        IntNode(9))))

  List(
    "  preorder: " -> tree.preorder _, // `_` denotes the function value of type `IntNode => Unit` (returning nothing)
    "   inorder: " -> tree.inorder _,
    " postorder: " -> tree.postorder _,
    "levelorder: " -> tree.levelorder _) foreach {
      case (name, func) =>
        val s = new StringBuilder(name)
        func(n => s ++= n.value.toString + " ")
        println(s)
    }
}


NSString *text = @"Hello,How,Are,You,Today";
NSArray *tokens = [text componentsSeparatedByString:@","];
NSString *result = [tokens componentsJoinedByString:@"."];
NSLog(result);


let words = String.split_on_char ',' "Hello,How,Are,You,Today" in
String.concat "." words


text='Hello,How,Are,You,Today'
do while text \= ''
   parse var text word1 ',' text
   call charout 'STDOUT:',word1'.'
end


(define (preorder tree)
  (if (null? tree)
      '()
      (append (list (car tree))
              (preorder (cadr tree))
              (preorder (caddr tree)))))

(define (inorder tree)
  (if (null? tree)
      '()
      (append (inorder (cadr tree))
              (list (car tree))
              (inorder (caddr tree)))))

(define (postorder tree)
  (if (null? tree)
      '()
      (append (postorder (cadr tree))
              (postorder (caddr tree))
              (list (car tree)))))

(define (level-order tree)
  (define lst '())
  (define (traverse nodes)
    (if (pair? nodes)
        (let ((next-nodes '()))
          (do ((p nodes (cdr p)))
              ((null? p))
            (set! lst (cons (caar p) lst))
            (let* ((n '())
                   (n (if (null? (cadar p))
                           n
                           (cons (cadar p) n)))
                   (n (if (null? (caddar p))
                           n
                           (cons (caddar p) n))))
              (set! next-nodes (append n next-nodes))))
          (traverse (reverse next-nodes)))))
  (if (null? tree)
      '()
      (begin
        (traverse (list tree))
        (reverse lst))))

(define (demonstration tree)
  (define (display-values lst)
    (do ((p lst (cdr p)))
        ((null? p))
      (display (car p))
      (if (pair? (cdr p))
          (display " ")))
    (newline))
  (display "preorder:    ") (display-values (preorder tree))
  (display "inorder:     ") (display-values (inorder tree))
  (display "postorder:   ") (display-values (postorder tree))
  (display "level-order: ") (display-values (level-order tree)))

(define the-task-tree
  '(1 (2 (4 (7 () ())
            ())
         (5 () ()))
      (3 (6 (8 () ())
            (9 () ()))
         ())))

(demonstration the-task-tree)


FUNCTION tokenizeString RETURNS CHAR (
   i_c AS CHAR
):

   DEF VAR ii        AS INT.
   DEF VAR carray    AS CHAR EXTENT.
   DEF VAR cresult   AS CHAR.

   EXTENT( carray ) = NUM-ENTRIES( i_c ).

   DO ii = 1 TO NUM-ENTRIES( i_c ):
      carray[ ii ] = ENTRY( ii, i_c ).
   END.

   DO ii = 1 TO EXTENT( carray ).
      cresult = cresult + "." + carray[ ii ].
   END.
   RETURN SUBSTRING( cresult, 2 ).

END FUNCTION. /* tokenizeString */

MESSAGE
   tokenizeString( "Hello,How,Are,You,Today" )
VIEW-AS ALERT-BOX.


for T in {String.tokens "Hello,How,Are,You,Today" &,} do
   {System.printInfo T#"."}
end


\\ Tokenize a string str according to 1 character delimiter d. Return a list of tokens.
\\ Using ssubstr() from http://rosettacode.org/wiki/Substring#PARI.2FGP
\\ tokenize() 3/5/16 aev
tokenize(str,d)={
my(str=Str(str,d),vt=Vecsmall(str),d1=sasc(d),Lr=List(),sn=#str,v1,p1=1);
for(i=p1,sn, v1=vt[i]; if(v1==d1, listput(Lr,ssubstr(str,p1,i-p1)); p1=i+1));
return(Lr);
}

{
\\ TEST
print(" *** Testing tokenize from Version #1:");
print("1.", tokenize("Hello,How,Are,You,Today",","));
\\ BOTH 2 & 3 are NOT OK!!
print("2.",tokenize("Hello,How,Are,You,Today,",","));
print("3.",tokenize(",Hello,,How,Are,You,Today",","));
}


main(args(2)) :=
    "preorder: " ++ toString(preOrder(testTree)) ++
    "\ninoder: " ++ toString(inOrder(testTree)) ++
    "\npostorder: " ++ toString(postOrder(testTree)) ++
    "\nlevel-order: " ++ toString(levelOrder(testTree));

Node ::= (value : int, left : Node, right : Node);

preOrder(n) := [n.value] ++
               (preOrder(n.left) when isDefined(n, left) else []) ++
               (preOrder(n.right) when isDefined(n, right) else []);

inOrder(n) :=  (inOrder(n.left) when isDefined(n, left) else []) ++
               [n.value] ++
               (inOrder(n.right) when isDefined(n, right) else []);

postOrder(n) := (postOrder(n.left) when isDefined(n, left) else []) ++
                (postOrder(n.right) when isDefined(n, right) else []) ++
                [n.value];

levelOrder(n) := levelOrderHelper([n]);
levelOrderHelper(ns(1)) :=
    let
        n := head(ns);
    in
        [] when size(ns) = 0 else
        [n.value] ++ levelOrderHelper(tail(ns) ++
        ([n.left] when isDefined(n, left) else []) ++
        ([n.right] when isDefined(n, right) else []));

testTree :=
    (value : 1,
     left : (value : 2,
             left : (value : 4,
                     left : (value : 7)),
                     right : (value : 5)),
             right : (value : 3,
                      left : (value : 6,
                              left : (value : 8),
                              right : (value : 9))
             )
    );


program TokenizeString;

{$mode objfpc}{$H+}

uses
  SysUtils, Classes;
const
  TestString = 'Hello,How,Are,You,Today';
var
  Tokens: TStringList;
  I: Integer;
begin
  // Uses FCL facilities, "harder" algorithm not implemented
  Tokens := TStringList.Create;
  try
    Tokens.Delimiter := ',';
    Tokens.DelimitedText := TestString;
    Tokens.Delimiter := '.'; // For example
    // To standard Output
    WriteLn(Format('Tokenize from: "%s"', [TestString]));
    WriteLn(Format('to:            "%s"',[Tokens.DelimitedText]));
  finally
    Tokens.Free;
  end;
end.


print join('.', split /,/, 'Hello,How,Are,You,Today'), "\n";


/# "Hello,How,Are,You,Today" "," "." subst print #/
"Hello,How,Are,You,Today" "," " " subst split len for get print "." print endfor


<?php
$str = 'Hello,How,Are,You,Today';
echo implode('.', explode(',', $str));
?>


import util.

go =>
  S = "Hello,How,Are,You,Today",
  T = S.split(","),
  println(T),
  T.join(".").println(),

  % As a one liner:
  S.split(",").join(".").println().


(mapcar pack
   (split (chop "Hello,How,Are,You,Today") ",") )


"Protocol: visiting"
EmptyNode>>accept: aVisitor

EmptyNode>>accept: aVisitor with: anObject
	^anObject

"Protocol: enumerating"
EmptyNode>>traverse: aVisitorClass do: aBlock
	^self accept: (aVisitorClass block: aBlock)


("Hello,How,Are,You,Today" / ",") * ".";


tok: Proc Options(main);
declare s character (100) initial ('Hello,How,Are,You,Today');
declare n fixed binary (31);

n = tally(s, ',')+1;

begin;
   declare table(n) character (50) varying;
   declare c character (1);
   declare (i, k) fixed binary (31);

   table = ''; k = 1;
   do i = 1 to length(s);
      c = substr(s, i, 1);
      if c = ',' then k = k + 1;
      else table(k) = table(k) || c;
   end;

   /* display the table */
   table = table || '.';
   put skip list (string(table));
end;
end;


100H:
/* CP/M CALLS */
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
PRINT: PROCEDURE (S); DECLARE S ADDRESS; CALL BDOS(9,S); END PRINT;

/* SPLIT A STRING ON CHARACTER 'SEP'.
   THE 'PARTS' ARRAY WILL CONTAIN POINTERS TO THE START OF EACH ELEMENT.
   THE AMOUNT OF PARTS IS RETURNED.
*/
TOKENIZE: PROCEDURE (SEP, STR, PARTS) ADDRESS;
    DECLARE SEP BYTE, (STR, PARTS) ADDRESS;
    DECLARE (N, P BASED PARTS) ADDRESS;
    DECLARE CH BASED STR BYTE;
    N = 0;
LOOP:
    P(N) = STR;
    N = N + 1;
    DO WHILE CH <> '$' AND CH <> SEP;
        STR = STR + 1;
    END;
    IF CH = '$' THEN RETURN N;
    CH = '$';
    STR = STR + 1;
    GO TO LOOP;
END TOKENIZE;

/* TEST ON THE GIVEN INPUT */
DECLARE HELLO (24) BYTE INITIAL ('HELLO,HOW,ARE,YOU,TODAY$');
DECLARE PARTS (10) ADDRESS;
DECLARE (I, LEN) ADDRESS;

LEN = TOKENIZE(',', .HELLO, .PARTS);
DO I = 0 TO LEN-1;
    CALL PRINT(PARTS(I));
    CALL PRINT(.'. $');
END;

CALL EXIT;
EOF;


class TreeNode<T> {
    let value: T
    let left: TreeNode?
    let right: TreeNode?

    init(value: T, left: TreeNode? = nil, right: TreeNode? = nil) {
        self.value = value
        self.left = left
        self.right = right
    }

    func preOrder(function: (T) -> Void) {
        function(value)
        if left != nil {
            left!.preOrder(function: function)
        }
        if right != nil {
            right!.preOrder(function: function)
        }
    }

    func inOrder(function: (T) -> Void) {
        if left != nil {
            left!.inOrder(function: function)
        }
        function(value)
        if right != nil {
            right!.inOrder(function: function)
        }
    }

    func postOrder(function: (T) -> Void) {
        if left != nil {
            left!.postOrder(function: function)
        }
        if right != nil {
            right!.postOrder(function: function)
        }
        function(value)
    }

    func levelOrder(function: (T) -> Void) {
        var queue: [TreeNode] = []
        queue.append(self)
        while queue.count > 0 {
            let node = queue.removeFirst()
            function(node.value)
            if node.left != nil {
                queue.append(node.left!)
            }
            if node.right != nil {
                queue.append(node.right!)
            }
        }
    }
}

typealias Node = TreeNode<Int>

let n = Node(value: 1,
             left: Node(value: 2,
                        left: Node(value: 4,
                                   left: Node(value: 7)),
                        right: Node(value: 5)),
             right: Node(value: 3,
                         left: Node(value: 6,
                                    left: Node(value: 8),
                                    right: Node(value: 9))))

let fn = { print($0, terminator: " ") }

print("pre-order:   ", terminator: "")
n.preOrder(function: fn)
print()

print("in-order:    ", terminator: "")
n.inOrder(function: fn)
print()

print("post-order:  ", terminator: "")
n.postOrder(function: fn)
print()

print("level-order: ", terminator: "")
n.levelOrder(function: fn)
print()


To run:
Start up.
Split "Hello,How,Are,You,Today" into some string things given the comma byte.
Join the string things with the period byte giving a string.
Destroy the string things.
Write the string on the console.
Wait for the escape key.
Shut down.

To join some string things with a byte giving a string:
Get a string thing from the string things.
Loop.
If the string thing is nil, exit.
Append the string thing's string to the string.
If the string thing's next is not nil, append the byte to the string.
Put the string thing's next into the string thing.
Repeat.