[R] Which data structure in R can mimic hash in PERL best?

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Sat Nov 15 20:42:31 CET 2008


Duncan Temple Lang wrote:
>
> Hans W. Borchers wrote:
>> Below I enclose a solution that I have written some month ago. It is
>> given in pythonic language, but you can easily translate it into the
>> corresponding Perl terms. One drawback is that one cannot use numbers
>> as keys, only keys following the naming conventions for variable names
>> are allowed.
>
> I imagine that you want to add inherits = FALSE
> to calls to exists() and get() in your code or
> else you risk quite unusual semantics of a hash table.

as far as i can see, an alternative is to use empty parent environments:

def.h <- function() new.env(hash=TRUE, parent=emptyenv())


vQ


>
>  D.
>
>>
>> Hans Werner Borchers
>> ABB Corporate Research
>>
>>
>> #-- Define functions on Hash Tuples (Python alike) ---------------------
>>
>> def.h <- function() new.env(hash=TRUE)
>> len.h <- function(dict) length(ls(envir=dict))
>> set.h <- function(key, val, dict) assign(key, val, envir=dict)
>> get.h <- function(key, dict, default=NULL) {
>>     if (exists(key, envir=dict)) { get(key, dict)
>>     } else { default } }
>> has_key <- function(key, dict) exists(key, envir=dict)
>> keys.h <- function(dict) ls(envir=dict)
>> items.h <- function(dict) as.list(dict)
>> values.h <- function(dict, mode='character') {
>>     l <- as.list(dict)
>>     n <- length(l)
>>     if (n==0) invisible(NULL)
>>     v <- vector('character', n)
>>     for (i in 1:n) v[i] <- l[[i]]
>>     if (mode=='numeric') v <- as.numeric(v)
>>     return(v)
>> }
>> del.h <- function(key, dict) {
>>     if (exists(key, envir=dict)) {
>>         val <- get.h(key, dict)
>>         rm(list=c(key), envir=dict)
>>     } else {
>>         val <- NULL
>>     }
>>     invisible(val)
>> }
>> clear.h <- function(dict) {
>>     rm(list=keys.h(dict), envir=dict)
>> }
>> #-----------------------------------------------------------------------
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
   

-- 
-------------------------------------------------------------------------------
Wacek Kusnierczyk, MD PhD

Email: waku at idi.ntnu.no
Phone: +47 73591875, +47 72574609

Department of Computer and Information Science (IDI)
Faculty of Information Technology, Mathematics and Electrical Engineering (IME)
Norwegian University of Science and Technology (NTNU)
Sem Saelands vei 7, 7491 Trondheim, Norway
Room itv303

Bioinformatics & Gene Regulation Group
Department of Cancer Research and Molecular Medicine (IKM)
Faculty of Medicine (DMF)
Norwegian University of Science and Technology (NTNU)
Laboratory Center, Erling Skjalgsons gt. 1, 7030 Trondheim, Norway
Room 231.05.060



More information about the R-help mailing list