Pascal:Delphi 长度字符串命令错误

发布于 2024-10-26 18:01:12 字数 671 浏览 1 评论 0原文

我正在编写一段代码来读取 CSV 文件并从中解析信息(目前我只有代码的开始部分,它将读取文件开头的标题。当我尝试编译此代码时我在获取文件行长度的行上收到错误

:[Error] MCLRandomizer.pas(*):缺少运算符或分号。

while not EOF(csvFile) do begin
        i :=0;
        ReadLn(csvFile, line);
        if lineOne = true then begin
          length := Length(line);               //error here
          while length > 0 do begin
            dx := Pos(',', line);
            buffer := Copy(line, 0, dx-1);
            headers[i] := buffer;
            line := Copy(line, dx+1, length);   
            length := Length(line);             //error here
          end;
          lineOne := false;
        end;
      end;

I'm writing a section of code to read in CSV files and parse information out of them (currently I just have the beginning part of the code which will read in the headers at the beginning of the file. When I try to compile this code I'm receiving an error on the line which takes the length of the line from file.

The error I'm recieving is: [Error] MCLRandomizer.pas(*): Missing operator or semicolon

while not EOF(csvFile) do begin
        i :=0;
        ReadLn(csvFile, line);
        if lineOne = true then begin
          length := Length(line);               //error here
          while length > 0 do begin
            dx := Pos(',', line);
            buffer := Copy(line, 0, dx-1);
            headers[i] := buffer;
            line := Copy(line, dx+1, length);   
            length := Length(line);             //error here
          end;
          lineOne := false;
        end;
      end;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千纸鹤 2024-11-02 18:01:12

Pascal 对 length 和 Length 没有区别...它们都是 LENGTH

重命名变量,它会弄乱函数。

Pascal makes no difference between length and Length ... they both are LENGTH

Rename the variable, it messes up the function.

浊酒尽余欢 2024-11-02 18:01:12

FTR:如果你真的、真的想要你可以写

length := System.Length(line);

(假设length是一个整数)。我同意其他海报的观点,这将是一个坏主意。

FTR: If you really, really want you can write

length := System.Length(line);

(assuming length is an Integer). I agree with the other posters that that would be a bad idea.

过气美图社 2024-11-02 18:01:12

我开发的将 csv 文件读入记录结构(实际上是记录结构数组)的解决方案是

program read_file_into_array_of_records;

{$APPTYPE CONSOLE}

uses
  SysUtils, StrUtils;



type
    Tscore = record
                name : string [25];
                marks : integer;
              end;

var
    input_file: TextFile;
    file_record : string[100];
    score : array [0..3] of Tscore;
    index : integer;



// function that returns all text up to a comma or the end of the line
function get_value() : string;
var
    comma_pos: integer;
    value: string[100];
begin


    comma_pos := Pos(',', file_record);

    // if comma found cut out all text up to it
    if comma_pos <> 0 then
    begin


        value := leftstr(file_record, comma_pos - 1);


        delete(file_record, 1, comma_pos);
    end
    else
    begin

        // no comma found so just take everything that remains
        value := file_record;
    end;

    get_value := value;


end;


// procedure to fill one record by breaking up the comma separated values
procedure fill_record (index: integer);
begin

    // call the function get_value as many times as needed to get
    // each comma separated value
    score[index].name := get_value();
    score[index].marks := strtoint(get_value());
end;


// procedure to fill array with contents of csv file
procedure fill_array ();
begin


    index := 0;



    while not EoF(input_file) do
    begin


        readln(input_file, file_record);


        fill_record (index);


        index := index + 1;
    end;


end;


// procedure to display contents of array
procedure display_array ();
begin



    for index := 0 to 3 do
    begin


        writeln(score[index].name, ' got ', score[index].marks, ' marks' );
    end;

    readln;

end;


// main prog

begin

    assignfile(input_file, 'scores.csv');

    reset(input_file);

    fill_array ();

    closefile(input_file);

    display_array();

end.

Scores.csv 的内容:

詹姆斯,31
简,23
托比,34
露丝,40

A solution I developed to read a csv file into a record structure (actually an array of records structures) is

program read_file_into_array_of_records;

{$APPTYPE CONSOLE}

uses
  SysUtils, StrUtils;



type
    Tscore = record
                name : string [25];
                marks : integer;
              end;

var
    input_file: TextFile;
    file_record : string[100];
    score : array [0..3] of Tscore;
    index : integer;



// function that returns all text up to a comma or the end of the line
function get_value() : string;
var
    comma_pos: integer;
    value: string[100];
begin


    comma_pos := Pos(',', file_record);

    // if comma found cut out all text up to it
    if comma_pos <> 0 then
    begin


        value := leftstr(file_record, comma_pos - 1);


        delete(file_record, 1, comma_pos);
    end
    else
    begin

        // no comma found so just take everything that remains
        value := file_record;
    end;

    get_value := value;


end;


// procedure to fill one record by breaking up the comma separated values
procedure fill_record (index: integer);
begin

    // call the function get_value as many times as needed to get
    // each comma separated value
    score[index].name := get_value();
    score[index].marks := strtoint(get_value());
end;


// procedure to fill array with contents of csv file
procedure fill_array ();
begin


    index := 0;



    while not EoF(input_file) do
    begin


        readln(input_file, file_record);


        fill_record (index);


        index := index + 1;
    end;


end;


// procedure to display contents of array
procedure display_array ();
begin



    for index := 0 to 3 do
    begin


        writeln(score[index].name, ' got ', score[index].marks, ' marks' );
    end;

    readln;

end;


// main prog

begin

    assignfile(input_file, 'scores.csv');

    reset(input_file);

    fill_array ();

    closefile(input_file);

    display_array();

end.

the contents of scores.csv:

james,31
jane,23
toby,34
ruth,40

止于盛夏 2024-11-02 18:01:12

此外,Pascal 字符串位于 1,而不是 0。(copy() 语句)

Moreover, Pascal strings at 1, not 0. (copy() statement)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文