Thanks Ford. Is there a reason you discourage using symbols? I opted for 
the symbols partly because I couldn't get your example to work. So I 
combined code from the various answers until something worked. 

I also noticed some odd behavior with the present implementation and I 
think its part of the reason I am having difficulty understanding all of 
the syntax. For example, overwriting values works both ways: 
population[:history,1][1] = 20 or population[:history][1][1] = 20. However 
the other properties do not work with both indexing schemes. It only works 
with population[:infected,1] = 20. 

On Saturday, June 4, 2016 at 3:11:18 PM UTC-4, Ford O. wrote:
>
> I would like to discourage you from passing symbols as an array index but 
> I guess you gonna do it anyway...
>
>
> So if you really want it, here it is:
> setindex!(p::Population, value, field::Symbol, index...) = p.individuals[
> index...].(field) = value
> getindex(p::Population, field::Symbol, index...) = p.individuals[index
> ...].(field)
>
> Usage
> population[:infected, 1]
> population[:infected, 1] = false
>
>
>
>
> On Saturday, June 4, 2016 at 2:19:02 PM UTC+2, Christopher Fisher wrote:
>>
>> I was wondering if someone would be willing to help me with creating 
>> user-defined types. I've been using Julia for about two years now but I am 
>> new to the idea of creating custom types. I'm trying to create a population 
>> of agents/individuals in a simple epidemiological simulation. I would like 
>> the population of individuals to be structured as a  2 dimensional array 
>> with rows as individuals and columns as properties. This would be somewhat 
>> similar to a DataFrame, but potentially more flexible. I want to be able to 
>> index an individual like so: population[1]. This woud list all of the 
>> information for individual 1.  I would also like to be able to look at an 
>> attribute across individuals: population.infected or population[:infected]. 
>> At the same time, I would like to have to flexibility of using an array to 
>> keep track of individuals: typeof(population.history[1]) is Array{Int64,1}. 
>> Based on existing documentation and examples, I have only been able to 
>> create individuals but cannot figure out how to create a population as 
>> described above:
>>
>> type Person
>>     infected::Int64
>>     vaccinated::Int64
>>     dead::Int64
>>    history::Array{Int64,1}
>> end
>>
>> Any help would be greatly appreciated. 
>>
>

Reply via email to