store {parcr} | R Documentation |
Store and retrieve objects
Description
Sometimes you want to use a parsed object to modify a later parser operation,
as in the example below. The store()
and retrieve()
functions provide the
tools to create such a parser.
Usage
store(name, value)
retrieve(name)
Arguments
name |
a string used as the name of the stored object. |
value |
object to be stored. |
Value
Nothing for store()
and the stored object for retrieve()
.
Examples
parse_nr <- function(line) {
m <- stringr::str_extract(line, "number=(\\d+)", group=1)
if (is.na(m)) list()
else store("nr", as.numeric(m))
}
p <- function() {
match_s(parse_nr) %then%
exactly(retrieve("nr"), literal("A"))
}
p()(c("number=3", "A", "A", "A")) # success
p()(c("number=2", "A", "A", "A")) # failure
[Package parcr version 0.5.2 Index]