Ok. I didn't anticipate this would be a drawn out process.  I just Ford's 
suggestion fixed the issue of changing values but recreated the problem of 
accessing an individual's information like so:  population[2] . 
population[:,2] did not work either. I tried other changes to the syntax 
but to no avail. It fails at the last line.  Any ideas would be 
appreciated. 

type Person
    infected::Bool
    vaccinated::Bool
    dead::Bool
    history::Vector{Int}
end

type Population <: AbstractArray
    individuals::Vector{Person}
    
end


Population() = Population(Person[])
Base.push!(p::Population, x::Person) = push!(p.individuals, x)


function Base.getindex(pop::Population, field::Symbol)
    [getfield(x, field) for x in pop.individuals]
end

setindex!(p::Population, value, field::Symbol, index...) = 
p.individuals[index...].(field) = value
getindex(p::Population, field::Symbol, index...) = 
p.individuals[index...].(field)

Base.show(io::IO, p::Person) = print(io, "Person(infected=", p.infected, ", 
vaccinated=", p.vaccinated, ", dead=", p.dead, ", history=", p.history)


population = Population()
push!(population, Person(true, false, true, [3,4,5]))
push!(population, Person(false, true, false, Int64[3,4,5]))

#Show that it works
population[:infected]
population[:infected,1]
population[2]

LoadError: MethodError: `size` has no method matching size(::Population)
Closest candidates are:
  size(::Any, !Matched::Integer, !Matched::Integer, !Matched::Integer...)
while loading In[1], in expression starting on line 36

 in getindex at abstractarray.jl:488



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