#1 2025-12-18 23:42:18

rcla
Member
Registered: 2025-12-13
Posts: 10

Use of DynArrayLoadCsv

I have a "test.csv" file that only contains:

qty,price
4,50
12,100

I'm a beginner in mORMot2 and I'm trying to use the "DynArrayLoadCsv" function, but it always returns false:

type
  TCsvItem = packed record
    qty: integer;
    price: integer;
  end;
  TCsvItems = array of TCsvItem; 

var
  csvItems: TCsvItems;
  csvContent: RawUtf8;

  csvContent := RawUtf8FromFile('test.csv'); 

  if DynArrayLoadCsv(csvItems, csvContent, TypeInfo(TCsvItems)) then
    writeln('DynArrayLoadCsv OK')
  else
    writeln('DynArrayLoadCsv Failed'); 

I tried changing the delimiter in the csv file from "," to ";" but it's still the same.

This is the complete code for the simple test program, including the CSV file.

I would appreciate any guidance you can provide.

Offline

#2 2025-12-19 05:03:00

zen010101
Member
Registered: 2024-06-15
Posts: 159

Re: Use of DynArrayLoadCsv

initialization
    // Register RTTI before any CSV parsing
    Rtti.RegisterFromText([TypeInfo(TCsvItem), _TCsvItem]);

https://github.com/zen010101/Claude_Mor … array-demo

Last edited by zen010101 (2025-12-19 05:03:29)

Offline

#3 2025-12-19 18:41:33

rcla
Member
Registered: 2025-12-13
Posts: 10

Re: Use of DynArrayLoadCsv

zen010101 wrote:

initialization
    // Register RTTI before any CSV parsing
    Rtti.RegisterFromText([TypeInfo(TCsvItem), _TCsvItem]);

https://github.com/zen010101/Claude_Mor … array-demo


Thank you so much for your help and explanation, and the example you did with Claude is great.

One small detail I encountered with your example when opening it with FPC is that the executable path is in the "bin" folder, while the "test.csv" file is in the root directory.
And obviously, a message appears indicating that it can't be found.

The generated documentation is excellent: very concise and detailed in your example.


Thank you for taking the time to provide the solution and create an excellent example.

Offline

#4 2025-12-20 02:58:09

zen010101
Member
Registered: 2024-06-15
Posts: 159

Re: Use of DynArrayLoadCsv

on the program dir, run "./bin/CsvDynArrayDemo.exe" command in the terminal window. smile

Offline

#5 2025-12-20 10:04:24

rcla
Member
Registered: 2025-12-13
Posts: 10

Re: Use of DynArrayLoadCsv

Currently, the "DynArrayLoadCsv" function only allows csv file content to have a delimiter/separator ','.

IMHO, the function should have the option (not mandatory) to specify the delimiter/separator.

Respectfully, I propose changing the definition to: (It does not affect programs that have already been created.)

function TDynArrayLoadCsv(var Value: TDynArray; Csv: PUtf8Char;
  Intern: TRawUtf8Interning = nil; CommaSep: AnsiChar=','): boolean;

function DynArrayLoadCsv(var Value; const Csv: RawUtf8; TypeInfo: PRttiInfo;
  Intern: TRawUtf8Interning = nil; CommaSep: AnsiChar=','): boolean;

And its respective implementation code: (I've only changed the constant ',' to a variable "CommaSep: AnsiChar")

An example of using a CSV file containing the semicolon ';' delimiter/separator:


if DynArrayLoadCsv(csvItems, csvContent, TypeInfo(TCsvItems), nil, ';') then
begin
  
end;

Offline

#6 2025-12-20 11:07:46

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,347
Website

Re: Use of DynArrayLoadCsv

Offline

#7 2025-12-20 11:59:07

rcla
Member
Registered: 2025-12-13
Posts: 10

Re: Use of DynArrayLoadCsv

Thank you for including it.

Offline

Board footer

Powered by FluxBB