Array of structs

question

#1

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.


#2

What part of the code do you have problems with? Loops, assignments, creating random numbers?

Also, please consider wrapping your code into backticks, e.g.

```
your code here
```

#3

From variables declarations (var…) to the end of Pascal code. In other words I asked the translation of Pascal code to Julia.


#4

Construct an array of structs with size maxpoints simply by saying

array_of_points_2d = Array{Point2D}(maxpoints)

#5

It is already done in the code I sent, the question is how to declare a variable of that type and use it as in Pascal code.


#6

What specific part is the problem?


#7

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.


#8
type xnode;
  centroid;
  splitvec;
  xparent;
  pscore;
  xdep;
  end;
  
function collectxnode(cents, xenc, xpar, pscores, xdep)
  n = size(cents,1);
  x = [];
  for(iter = 1:n)
    push!(x, xnode(cents[iter, :], xenc .== iter, xpar, pscores[iter], xdep));
    end;
  return(x);
  end;