Page 1 of 1

Associative arrays in HOC

Posted: Fri Dec 13, 2019 5:36 pm
by vogdb
In Python we have dicts like this

Code: Select all

d = {'key1': 1, 'key2': 56}
Is there an analogue in HOC?

Re: Associative arrays in HOC

Posted: Mon Dec 16, 2019 11:30 am
by ted
Nope, hoc doesn't have dicts. You might be able to fake it with a pair of Lists.

Re: Associative arrays in HOC

Posted: Wed Dec 18, 2019 5:52 am
by vogdb
There is an implementation of Dictionary in this post viewtopic.php?t=1438

Re: Associative arrays in HOC

Posted: Wed Dec 18, 2019 7:20 am
by vogdb
Also here a couple of examples:

Code: Select all

begintemplate String
   public s
   strdef s
endtemplate String

begintemplate ObjDict
  public get, put, setKeys
  objref keyList, valueList, nil

  proc init() {
    keyList = new List()
    valueList = new List()
  }

  proc put() {localobj key
    key = new String()
    key.s = $s1
    keyList.append(key)
    valueList.append($o2)
  }

  obfunc get() {local i localobj key
    for i=0, keyList.count()-1 {
      if( strcmp( keyList.o(i).s, $s1 ) == 0 ) {
        return valueList.o(i)
      }
    }
    return nil
  }

endtemplate ObjDict

begintemplate NumDict
  public get, put, setKeys
  objref keyList, valueList

  proc init() {
    keyList = new List()
    valueList = new Vector()
  }

  proc put() {localobj key
    key = new String()
    key.s = $s1
    keyList.append(key)
    valueList.append($2)
  }

  func get() {local i localobj key
    for i=0, keyList.count()-1 {
      if( strcmp( keyList.o(i).s, $s1 ) == 0 ) {
        return valueList.get(i)
      }
    }
  }

endtemplate NumDict

objref od, nd, vv, vvv
od = new ObjDict()
vv = new Vector()
vv.insrt(0,6,3,4)
od.put("hello", vv)
vvv = od.get("hello")
print vvv.size()

nd = new NumDict()
nd.put("hello", 5)
print nd.get("hello")


Re: Associative arrays in HOC

Posted: Wed Jan 01, 2020 2:13 pm
by ramcdougal
As long as Python is installed, you can use Python dictionaries from inside HOC:

Code: Select all

oc>objref py, d
oc>py = new PythonObject()
oc>d = py.dict()
oc>d.__setitem__("axon", 100)
	NULLobject 
oc>d.__setitem__("soma", 10)
	NULLobject 
oc>d.get("soma")
	10 
oc>d.get("axon")
	100 

Re: Associative arrays in HOC

Posted: Wed Jan 01, 2020 7:48 pm
by ted
As long as Python is installed, you can use Python dictionaries from inside HOC
This looks like the way to go--avoids trying to use hoc to replicate existing Python functionality.