Hi All,
I have some problems using Julia arrays with structs. I need some examples
I tried to translate this Free Pascal code in Julia to compare their performance but I have had a lot of errors in Julia code:
const MaxPoints = 100;
type
Point2D = record
rank: integer;
x, y: double;
end;
ArrayOfPoints2D = array[1..MaxPoints] of Point2D;
CurveRec = record
pts: ArrayOfPoints2D;
AvarageHeight, surface, volume: double;
end;
// Now I can define some variables
var
pts: ArrayOfPoints2D;
C: CurveRec;
tempR1, tempR2: double;
st: string;
i: integer;
// Now I can use pts and C variables this way
begin
for i := 1 to MaxPoints do
begin
pts[i].rank := i;
tempR1 := 2.71828*random(100);
tempR2 := 3.14159*random(50);
pts[i].x := tempR1;
pts[i].y := tempR2;
C.pts[i].x := pts[i].x;
C.pts[i].y := pts[i].y;
//...
end;
end.
const MaxPoints = 1024
type Point2D
x::Float64
y::Float64
end
type ArrayOfPoints2D Array{Point2D}(MaxPoints) end
type CurveRec
pts::ArrayOfPoints2D
AvarageHeight::Float64
surface::Float64
volume::Float64
end
How to transalate the remaining Pascal code in Julia?
Thank you.
Julia is dynamically typed and doesn’t require preliminary variable declaration. If you want a variable x of type Int with initial value 42, you can just write:
x = 42
Note, that we didn’t declare type of x, because type is an attribute of a value, not variable (although you can explicitly ensure the type if local variable using syntax local x::Int = 42).
If you want to construct an array of points instead of an integer, you can do it exactly with the syntax proposed by @mbauman:
array_of_points_2d = Array{Point2D}(maxpoints)
For further reference, I highly recommend reading the basics of the language, at least something like Learn Julia in Y Minutes. Julia community is highly friendly, but as any community, it focuses on helping people to learn the language, not on doing some work for them. And the best resources for learning are tutorials and books.