Last data update: 2014.03.03

R: forKeyVal - Looping named lists
forKeyValR Documentation

forKeyVal – Looping named lists

Description

forKeyVal loops a named list or vector, with one variable bound to the key and another to the value.

Usage

forKeyVal(key,val,LIST,block,envir=parent.frame())

Arguments

key

the variable to be bound by the name of the element of the LIST

val

the variable to be bound by the value of the element of the LIST

LIST

a list

block

the block to be exectued, with this variables bound

envir

the environment in which the variables are bound and the block is executed

Details

It works much like a for(val in LIST) block with the difference. It however additionally binds key to the value name of the list element, but does not recognize continue or break statements. This might change at some point in future.
Empty or unbound names result in a binding of key to NULL.

Value

the value of the last execution of the block

Author(s)

K. Gerald van den Boogaart

See Also

for, lapply

Examples

forKeyVal(name,x,c(a=1,b=5,c=6),{
  cat(name,"=>",x,"\n")
})

forKeyVal(name,x,list(a=4,b=1:7,c=c(a="Aber",b="nicht")), {
  cat(name,"\n",sep="")
  if(is.null(names(x)))
    names(x) <- 1:length(x)
  forKeyVal(iname,x,x,{
    cat(name,".",iname,"=>",x,"\n",sep="")
  })
})

Results