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.

key_index
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 = "")
  1. key_index ← 1

    1key_index <- 12codes <- c(ok = 200, missing = 404, error = 500)
    values this step1key_index
  2. codes ← 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=500codes
  3. key ← ok

    2codes <- c(ok = 200, missing = 404, error = 500)3key <- c("ok", "missing", "error")[key_index]4value <- codes[[key]]
    values this stepokkey1key_index
  4. value ← 200

    3key <- c("ok", "missing", "error")[key_index]4value <- codes[[key]]5label <- paste(key, value, sep = ":")
    values this step200valueokkey3 codescodes
  5. label ← ok:200

    4value <- codes[[key]]5label <- paste(key, value, sep = ":")6cat(label, "\n", sep = "")
    values this stepok:200labelokkey200value
  6. cat(label, " ", sep = "")

    5label <- paste(key, value, sep = ":")6cat(label, "\n", sep = "")
    outputok:200
    values this stepok:200label
  1. key_index ← 2

    1key_index <- 22codes <- c(ok = 200, missing = 404, error = 500)
    values this step2key_index
  2. codes ← 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=500codes
  3. key ← missing

    2codes <- c(ok = 200, missing = 404, error = 500)3key <- c("ok", "missing", "error")[key_index]4value <- codes[[key]]
    values this stepmissingkey2key_index
  4. value ← 404

    3key <- c("ok", "missing", "error")[key_index]4value <- codes[[key]]5label <- paste(key, value, sep = ":")
    values this step404valuemissingkey3 codescodes
  5. label ← missing:404

    4value <- codes[[key]]5label <- paste(key, value, sep = ":")6cat(label, "\n", sep = "")
    values this stepmissing:404labelmissingkey404value
  6. cat(label, " ", sep = "")

    5label <- paste(key, value, sep = ":")6cat(label, "\n", sep = "")
    outputmissing:404
    values this stepmissing:404label
  1. key_index ← 3

    1key_index <- 32codes <- c(ok = 200, missing = 404, error = 500)
    values this step3key_index
  2. codes ← 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=500codes
  3. key ← error

    2codes <- c(ok = 200, missing = 404, error = 500)3key <- c("ok", "missing", "error")[key_index]4value <- codes[[key]]
    values this steperrorkey3key_index
  4. value ← 500

    3key <- c("ok", "missing", "error")[key_index]4value <- codes[[key]]5label <- paste(key, value, sep = ":")
    values this step500valueerrorkey3 codescodes
  5. label ← error:500

    4value <- codes[[key]]5label <- paste(key, value, sep = ":")6cat(label, "\n", sep = "")
    values this steperror:500labelerrorkey500value
  6. cat(label, " ", sep = "")

    5label <- paste(key, value, sep = ":")6cat(label, "\n", sep = "")
    outputerror:500
    values 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.