TStringList 索引值的问题

发布于 2024-11-14 08:36:16 字数 1328 浏览 4 评论 0原文

因此,我有几个要读取并从中获取值的摘要文件。

我正在执行以下操作:

OutputSummary := TStringList.Create;
for idx := 0 to 82 do
  OutputSummary.Insert(idx, '');

为了初始化我正在使用的值

,我有一个循环:

for idx := 0 to SummaryFiles.Count - 1 do
  begin
    AssignFile(finp, SummaryFiles[idx]);
    ReSet(finp);

    for ndx := 0 to 5 do
      ReadLn(finp, buff);

    for ndx := 0 to 82 do   
      begin
        ReadLn(finp, buff);
        temp := GetToken(buff, ' ');
        buff := GetRemains(buff, '|');
        temp := GetToken(buff, '|');
        valuestring := OutputSummary[ndx] + delimiter + temp;
        OutputSummary.Insert(ndx, valuestring);

      end;

    CloseFile(finp);

end;  

第一个 0 到 5 循环跳过我不想读取的行,而 0 到 82 读取看起来像

1. Initial Wait List|1770

这样的行所以我正在调试程序,看看它如何仅使用 2 个摘要文件来工作。

第一次通过,效果非常好。该行被正确读取,我得到了值,当我插入 valuestring 时,它看起来像“,1770”(例如),我还可以在插入命令后突出显示 OutputSummary[ndx] 并查看该值是否已正确插入。

然后我打开第二个文件,该文件也可以正常工作

valuestring := OutputSummary[ndx] + delimiter + temp;

,直到第一次的行 OutputSummary[0] 正确并添加了正确的行。

但是,OutputSummary[1] 到 OutputSummary[82] 与 OutputSummary[0] 相同!这是没有意义的,因为当我第一次添加这些值时,我可以看到 OutputSummary[1] 到 82 是唯一且正确的。

任何人都可以看到问题吗?是调试器错误吗?我是否错过了一些我看不到的明显的东西?

谢谢

So I have several summary files that I want to read and get the values from.

I am doing the following:

OutputSummary := TStringList.Create;
for idx := 0 to 82 do
  OutputSummary.Insert(idx, '');

to initialize the values I'm using

then, I have a loop:

for idx := 0 to SummaryFiles.Count - 1 do
  begin
    AssignFile(finp, SummaryFiles[idx]);
    ReSet(finp);

    for ndx := 0 to 5 do
      ReadLn(finp, buff);

    for ndx := 0 to 82 do   
      begin
        ReadLn(finp, buff);
        temp := GetToken(buff, ' ');
        buff := GetRemains(buff, '|');
        temp := GetToken(buff, '|');
        valuestring := OutputSummary[ndx] + delimiter + temp;
        OutputSummary.Insert(ndx, valuestring);

      end;

    CloseFile(finp);

end;  

The first 0 to 5 loop skips the lines I don't want to read, and the 0 to 82 reads lines that look like

1. Initial Wait List|1770

So I was debugging the program to see how it works with just 2 SummaryFiles.

The first time through, it works perfectly. The line is read correctly, I get the value and when I insert valuestring, it looks like ",1770" (for example), and I can also highlight OutputSummary[ndx] after the insert command and see that the value was inserted correctly.

Then I open the second file, which also works fine until the line

valuestring := OutputSummary[ndx] + delimiter + temp;

the first time, OutputSummary[0] is correct and the correct line is added.

However, OutputSummary[1] through OutputSummary[82] is the same as OutputSummary[0]! This makes no sense since when I was first adding those values, I could see that OutputSummary[1] through 82 were unique and correct.

Can anyone see a problem? Is it a debugger error? Am I just missing something obvious that I don't see?

thanks

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

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

发布评论

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

评论(1

如何视而不见 2024-11-21 08:36:16

在我看来,您似乎正在尝试创建某种类型的表,每个输入文件一列,文件中每行一行,各列由分隔符分隔。如果是这样,在字符串列表上调用 .Insert 将无法正常工作,因为您最终将插入 83 * SummaryFiles.Count 行。

您需要的不是 Insert 调用,而是类似的内容:

if OutputSummary.count > ndx then
  OutputSummary[ndx] := valuestring
else OutputSummary.Add(valuestring);

看看是否有帮助。

另外,您可能需要考虑将“幻数”82 替换为有意义的常量,例如 const LINES_TO_READ = 82。这使得阅读代码并理解它应该做什么变得更容易。

It looks to me like you're trying to create a table of some sort, with one column per input file and one row per line in the file, with the columns separated by the delimiter. If so, calling .Insert on the string list isn't going to quite work right, since you'll end up inserting 83 * SummaryFiles.Count rows.

Instead of the Insert call, you need something like this:

if OutputSummary.count > ndx then
  OutputSummary[ndx] := valuestring
else OutputSummary.Add(valuestring);

See if that helps.

Also, you might want to consider replacing the "magic number" 82 with a meaningful constant, like const LINES_TO_READ = 82. That makes it easier to read the code and understand what it's supposed to be doing.

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