Calculating/Averaging over a struct value
Brian Brady
brian.brady1982 at gmail.com
Wed Mar 21 10:02:05 PDT 2012
All
This might be relatively trivial so please point me at documentation to read
if it is.
I am creating an array of Structs(is this the best thing to do) as per the
example below.
#! /usr/bin/rdmd
import std.array;
import std.csv;
import std.stdio;
import std.string;
struct Data
{
string Date;
double d1;
double d2;
int i4;
}
double average(Data[] x, string para)
{
if (para != "d1")
return -1;
else
{
// Here I want to dynamically average over whatever was entered as para
// but if I write x[].para if throws an error
foreach(i;0 .. x[].length)
writeln("bob");
return 1;
}
}
void main()
{
Data[] allData;
string text= "2011-11-30,2.25,3.00,100\n2011-11-29,2.24,2.75,1000\r\n";
foreach(ob;csvReader!Data(text))
{
allData ~= ob;
}
writeln(allData);
string input;
writeln("Enter the variable you would like to average over: ");
stdin.readln(input);
writeln(input);
}
As per the commented section, I want to be able to dynamically figure out,
which member of the struct to average across, for all the structs in the array.
Is there any way of doing this, or will I have to code one for each possibility.
ie.
if(para = "d1")
... calculate the average of x[].d1
else if(para = "d2")
... calculate the average of x[].d2
Is there a better method of doing this that I can look at?
Thanks in advance.
Brian
More information about the Digitalmars-d-learn
mailing list