Learn R Programming

gmDatabase (version 0.5.0)

forKeyVal: 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())

Value

the value of the last execution of the block

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

Author

K. Gerald van den Boogaart

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.

See Also

Examples

Run this code
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="")
  })
})

Run the code above in your browser using DataLab