[Translating] from Pascal to Julia

is it possible to have similar code in julia?
thanks

“type
lo=array[1…5,1…10]of smallint;
nom=string[25];
lot=record
num:lo;
ind:smallint;
anno:smallint;
mese:string[15];
end;
fi=file of lot;
var
f2,f3:fi;”

I think you’re going to have to explain what that code does in order to get a helpful answer to your question.

binary file composed of structures, each structure is composed of variables and a 5X10 matrix.

You want to write a series of binary records with a certain format, and read it back in?

yes yes

@fabbius69 yes, you could have an exact translation, or similar, here partially similar I think:

struct lo
  lo::Array{Int16, 2} # You can have static arrays with (5, 10) dimensions using StaticArrys.jl package, but might not be needed, unless you want to read a specific format.
  nom::String          # It's better to not limit length of the strings, not needed.
end

struct lot
  num::lo  # This isn't really helpful to split across structures, unless you want here an Array of lo.
  ind::Int16
  anno::Int16
  mese::String
end

Something like:
julia> x = lot(lo([1 2; 3 4], "test"), 1, 1, "Palli")

julia> using FileIO
julia> save("example.jld2", "x", x)  # this was a bit slower then with Serialization, but likely just on first use.

or alternatively:
julia> using Serialization
julia> open(f -> serialize(f,x), "x.jls", "w")
1 Like

Does it have to have a certain format because compatibility with other software, or do you just want to be able to read and write stuff to a file in some format?

thanks, but how do I create the file to be stored with this structure?

(1) Julia arrays have dynamic sizes. The size is not part of the type of the array, unlike in Pascal. Statically-sized arrays, when needed for specific purposes, are available from the package StaticArrays.jl.

(2) The size of a string in Julia is not part of its type, either.

(3) Pascal’s smallint is Julia’s Int16. Pascal’s record is roughly Julia’s struct.

(4) Julia does not have a file type for storing data of a single type. Instead you use a more “dynamically typed” standard library called Serialization.

4 Likes

You might want to use JLD2.jl package rather than Serialization, though the latter is built-in…

I create a database by entering this data from time to time, over time the data increases. I often read all the data and store them in array structures and do some processing.

If you have to be able to read files in a specific binary format that exist already and convert them into Julia structs… this is a more difficult task. If you are free to read and write any format, then Serialization library is I think the way to go.

Serialization format is not stable across versions, so it is not suitable for storing data for long periods.

Edit: Incorrect, see below

where I find a guide on the Serialization library

Well it is guaranteed that later Julia versions may read data produced by older Julia versions…

1 Like

Ah you’re right

The data format can change in minor (1.x) Julia releases, but files written by prior 1.x versions will remain readable.

1 Like

https://docs.julialang.org/en/v1/stdlib/Serialization/

yes, the big issue is if you need to send data to someone running an older Julia version.

Another option is to write the data to Arrow or Parquet formats… Arrow is good for writing binary data in large quantities and reading it in super fast. Parquet is more for compressed long term storage.

3 Likes

a guide for FileIO?
thanks

Yes, in general it’s best to add .jl to package names (inbuilt Julia modules should be documented in the Julia manual) when googling:

julia FileIO.jl

and from there click on docs. At least this package has good docs, and you could have rather done using JLD2, and that package might have the docs…