Codebase list libmawk / 7ab313a7-f14c-465a-bfe5-5a4585f9ed23/main scconfig / src / tmpasm / regression / Tutor07_sub.gasm
7ab313a7-f14c-465a-bfe5-5a4585f9ed23/main

Tree @7ab313a7-f14c-465a-bfe5-5a4585f9ed23/main (Download .tar.gz)

Tutor07_sub.gasm @7ab313a7-f14c-465a-bfe5-5a4585f9ed23/mainraw · history · blame

# The following regex sub utils are scconfig specific.

# Regex: substitute the first match of a pattern with str in a variable:
#   sub address pattern str

put myvar {Hello world!\n}
sub myvar {l} {2}
print myvar

# Address must resolve to an existing variable, pattern and str are data;
# this means address can be a string that holds a variable name, it's the
# same as if it was an addreess:
sub {myvar} {l} {3}
print myvar

# Or it can be a block, which makes indirect addressing possible:
# in [@@pointer@@] the @pointer@ part will be substituted with
# the value of pointer, which is "myvar".
put pointer {myvar}
sub [@@pointer@@] {l} {4}
print myvar

# Since pattern and str are also data, address and blocks work there as well
# (but this is _not_ a regex backreference):
put punctuation {[!?.]}
sub [@@pointer@@] punctuation [@ PUNCT:@punctuation@@]
print myvar

# gsub does the same, but substutites all matches, not only the first:
gsub [@@pointer@@] {o} {_0_}
print myvar