Skip to content
David-Apps edited this page Dec 8, 2021 · 14 revisions

The ldc function sends a command to dc for each number on a line.

Suppose that the current line is: 1 2 3 4 The command <ldc 1 + 2 ^ changes the line to: 4 9 16 25

# Send each number on a line to dc
# usage: <ldc [commands]
# This function uses the labels y and z to keep track of the current line.
function+ldc {
db0
# Check whether the current line contains a number.
s/\d/&/
if(*) {
-ky
if(*) {
s/-?\d+\(\.\d*\)?/\n&\n/g
kz
'y+,'zg/^-?\d+\(\.\d*\)?$/<dc ~0
'y+,'zj
} else {
s/-?\d+\(\.\d*\)?/\n&\n/g
kz
1,'zg/^-?\d+\(\.\d*\)?$/<dc ~0
1,'zj
}
} else {
r !echo '~0 p' | dc
}
p
db1
}

# Send a command to dc
# usage: <dc [commands]
# If the current line contains a number, this function sends the current line before the command.
function:dc {
s/\d/&/
if(*) {
s/^-\(\d+\(\.\d*\)?\)$/_$1/
r !echo ''. ~0 p' | dc
-d
} else {
r !echo '~0 p' | dc
}
}