forKeyVal: forKeyVal - Looping named lists

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/Database.R

Description

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

Usage

1
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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="")
  })
})

Example output

Loading required package: DBI
Loading required package: RMySQL
Loading required package: foreach
Loading required package: digest
Loading required package: shiny
gmDatabase: Working with geometallurgical databases,  Version: 0.5.0
a => 1 
b => 5 
c => 6 
a
a.1=>4
b
b.1=>1
b.2=>2
b.3=>3
b.4=>4
b.5=>5
b.6=>6
b.7=>7
c
c.a=>Aber
c.b=>nicht

gmDatabase documentation built on May 2, 2019, 9:31 a.m.