String Processing¶
Unicon String Processing¶
String Scanning¶
Unicon
string scanning is a very powerful computational feature of the
language. With roots dating back to SNOBOL, string scanning was added
to Icon by the team lead by Ralph Griswold as a modernization of the
pattern matching features of SNOBOL
. Unicon
has gone full circle and
starting with the release 13 beta builds, SNOBOL
Patterns are
actually part of the system again. And to add to the already rich options,
Regular expressions are also available now. A trifecta.
Scanning¶
String scanning syntax is based on the scanning operator ? (string scan).
expr1 ? expr2
Where the result of expr1 sets a scanning environment, &subject and &pos specifying the subject characters and current position of the scanning cursor. expr2 can be any valid Unicon expression, usually a sequence, and can range from simple to very complex selection and side effect operations.
A “simple” scan. Find Waldo.
#
# simple-scan.icn, A simple string scanning example
#
procedure main()
s := "Where's Waldo?"
s ? p := find("Waldo")
if \p then write("Found starting at position ", p)
end
Sample run:
prompt$ unicon -s simple-scan.icn -x
Found starting at position 9
A middling scan. Find name from command line (or Waldo).
#
# middling-scan.icn, A slightly less simple string scanning example
#
procedure main(arglist)
who := \arglist[1] | "Waldo"
"Where's " || map(who) ? {
write("Subject: ", image(&subject))
write("target : ", image(who))
p := find(map(who))
}
if \p then write("Found ", who, " starting at position ", p)
end
Sample run:
prompt$ unicon -s middling-scan.icn -x
Subject: "Where's waldo"
target : "Waldo"
Found Waldo starting at position 9
Todo
add string scanning samples
See: Patterns for a more on scanning.
Index | Previous: Development Tools | Next: Patterns