Advanced Data Structures
Lookup Table
Map Keys to Values
Named vectors are compact lookup tables when every value has the same type.
Program
Play the script to choose a key and retrieve the mapped status code.
lookup_table.R
Replay: real traced execution (multi-file project)
key_index <- 1
codes <- c(ok = 200, missing = 404, error = 500)
key <- c("ok", "missing", "error")[key_index]
value <- codes[[key]]
label <- paste(key, value, sep = ":")
cat(label, "\n", sep = "")
key_index <- 2
codes <- c(ok = 200, missing = 404, error = 500)
key <- c("ok", "missing", "error")[key_index]
value <- codes[[key]]
label <- paste(key, value, sep = ":")
cat(label, "\n", sep = "")
key_index <- 3
codes <- c(ok = 200, missing = 404, error = 500)
key <- c("ok", "missing", "error")[key_index]
value <- codes[[key]]
label <- paste(key, value, sep = ":")
cat(label, "\n", sep = "")
key_index ← 1
1key_index <- 12codes <- c(ok = 200, missing = 404, error = 500)values this step1key_indexcodes ← ok=200, missing=404, error=500
1key_index <- 12codes <- c(ok = 200, missing = 404, error = 500)3key <- c("ok", "missing", "error")[key_index]values this stepok=200, missing=404, error=500codeskey ← ok
2codes <- c(ok = 200, missing = 404, error = 500)3key <- c("ok", "missing", "error")[key_index]4value <- codes[[key]]values this stepokkey1key_indexvalue ← 200
3key <- c("ok", "missing", "error")[key_index]4value <- codes[[key]]5label <- paste(key, value, sep = ":")values this step200valueokkey3 codescodeslabel ← ok:200
4value <- codes[[key]]5label <- paste(key, value, sep = ":")6cat(label, "\n", sep = "")values this stepok:200labelokkey200valuecat(label, " ", sep = "")
5label <- paste(key, value, sep = ":")6cat(label, "\n", sep = "")outputok:200values this stepok:200label
key_index ← 2
1key_index <- 22codes <- c(ok = 200, missing = 404, error = 500)values this step2key_indexcodes ← ok=200, missing=404, error=500
1key_index <- 22codes <- c(ok = 200, missing = 404, error = 500)3key <- c("ok", "missing", "error")[key_index]values this stepok=200, missing=404, error=500codeskey ← missing
2codes <- c(ok = 200, missing = 404, error = 500)3key <- c("ok", "missing", "error")[key_index]4value <- codes[[key]]values this stepmissingkey2key_indexvalue ← 404
3key <- c("ok", "missing", "error")[key_index]4value <- codes[[key]]5label <- paste(key, value, sep = ":")values this step404valuemissingkey3 codescodeslabel ← missing:404
4value <- codes[[key]]5label <- paste(key, value, sep = ":")6cat(label, "\n", sep = "")values this stepmissing:404labelmissingkey404valuecat(label, " ", sep = "")
5label <- paste(key, value, sep = ":")6cat(label, "\n", sep = "")outputmissing:404values this stepmissing:404label
key_index ← 3
1key_index <- 32codes <- c(ok = 200, missing = 404, error = 500)values this step3key_indexcodes ← ok=200, missing=404, error=500
1key_index <- 32codes <- c(ok = 200, missing = 404, error = 500)3key <- c("ok", "missing", "error")[key_index]values this stepok=200, missing=404, error=500codeskey ← error
2codes <- c(ok = 200, missing = 404, error = 500)3key <- c("ok", "missing", "error")[key_index]4value <- codes[[key]]values this steperrorkey3key_indexvalue ← 500
3key <- c("ok", "missing", "error")[key_index]4value <- codes[[key]]5label <- paste(key, value, sep = ":")values this step500valueerrorkey3 codescodeslabel ← error:500
4value <- codes[[key]]5label <- paste(key, value, sep = ":")6cat(label, "\n", sep = "")values this steperror:500labelerrorkey500valuecat(label, " ", sep = "")
5label <- paste(key, value, sep = ":")6cat(label, "\n", sep = "")outputerror:500values this steperror:500label
named vector
A named vector maps character keys to same-type values.
lookup
`codes[[key]]` retrieves the value for a selected key.
map result
The final label checks both the key and returned value.