pascaltstringlistaltium-designerdelphiscript

Why does Pos() function return several values?


I'm currently working on a large script (for me) where I need to delete some files in a folder. To identify which files I need to delete, I'm reading a text file that contains all the information I need. I'm using a TStringList class and the function Pos() to get this information.

My problem comes from the function Pos() which is returning several values, one value for each string of the StringList (text file).

So here is the dedicated part of my script :

Procedure Delete;
Var
SL      : TStringList;
i, A, B : Integer;     //i is a Counter, A and B are not mandatory but are useful for DEBUG
PCBFile : String;
Begin
PCBFile := //Full Path
SL := TStringList.Create;
SL.LoadFromFile(PCBFile);
For i := 1 To (SL.Count - 1) Do         //I don't need to check the 1st line of the text file.
     A := Pos('gtl', SL.Strings[i]);    //Here is when the problem occurs.
     B := Pos('gbl', SL.Strings[i]);    //Doesn't get to here because i = SL.Count before looping
     If (A = 0) And (B = 0) Then        //The goal
          ChangeFileExt(PCBFile, '.TX');
          PCBFile := PCBFile + FloatToStr(i-2);     //The files I want to delete can have several extensions (tx1, tx2... txN)
          DeleteFile(PCBFile);
     End;
End;
SL.Free;
End;

If I change this line :

A := Pos('gtl', SL.Strings[i]);

To this :

ShowInfo (Pos('gtl', SL.Strings[i]));  //Basic function from Altium Designer, same as Writeln()

It will show a pop-up with the result of the function Pos() for each line of the text file. I assume that SL.Strings[i] does an internal auto-increment of i, but I want to manage it with my For Do loop.

I searched on the web but didn't find any clue on what to do, maybe the problem comes from Altium Designer, which is not really a programming software at first.

I also tried with the SL.IndexOf() function but it only works with strict Strings, which is not my case.


Solution

  • There are a few errors in the code, namely incomplete Begin .. End; blocks.

    The first one is the for i .. loop. If you want to execute several statements within every iteration of the loop you must enclose them in a Begin .. End; pair. Your code is missing the Begin and therefore it assigns A SL.Count-1 times before it reaches the line where B is assigned. The second is after the If .. statement. I you want to execute several statements conditionally you must enclose them in a Begin .. End; pair after the If .. statement.

    Add the two lines as marked below

    For i := 1 To (SL.Count - 1) Do         //I don't need to check the 1st line of the text file.
    Begin  // Add this line
        A := Pos('gtl', SL.Strings[i]);    //Here is when the problem occurs.
        B := Pos('gbl', SL.Strings[i]);    //Doesn't get to here because i = SL.Count before looping
        If (A = 0) And (B = 0) Then        //The goal
        Begin  // Add this line
            ChangeFileExt(PCBFile, '.TX');
            PCBFile := PCBFile + FloatToStr(i-2);     //The files I want to delete can have several extensions (tx1, tx2... txN)
            DeleteFile(PCBFile);
        End;
    End;
    

    Remember also that, in Pascal, indentation has no effect on how code executes (as it has in some other languages). Indentation is important for readability, but that is all.